[Tutorial] 'Welcome to my server' messages.
#1

Hey, how's everybody doing? In this BEGINNER FRIENDLY tutorial, I will teach you how to send a welcome message to a player of your server. For this tutorial, you pretty much need to know the basics of Pawno and you're ready to go. Plus, it's pretty easy to do. There are two tutorial in this one: a simple message and a message where you include a player's name. THIS IS MY FIRST TUTORIAL, SO PLEASE, TELL ME WHAT TO AMEND IN THE NEXT ONE.

__________________________________________
Step 1. Ontop of the script, we'll need to introduce we wish the message to be. In this case, I'll be using GREENYELLOW, but you can choose your own color here. For that, we'll need to define the color. I'll show you how to define a color:

pawn Code:
#define COLOR_YELLOWGREEN 0xADFF2FAA
NOTE: If you're using another color, keep #define and change the rest with the desired color.

Step 2. Now, for the welcome message, we'll need to introduce the public function 'OnPlayerConnect'. We do it like this:

pawn Code:
public OnPlayerConnect
Step 3. After introducing the public function, we need to introduce "(playerid)", because we're going to send a message to a player. It's pretty basic:

pawn Code:
public OnPlayerConnect(playerid)
Step 4. Easy so far? Now, we need to introduce the function "SendClientMessage", which sends a message to a player. You can read more about this function here.

pawn Code:
public OnPlayerConnect(playerid)
{
  SendClientMessage
Step 4.1. However, SendClientMessage has parameters you need to include. These parameters are as follows:
  1. playerid The playerid you would like to display the message for.
  2. color The color it should be in.
  3. const message[] The text you would like to display. max len displayable: 144
pawn Code:
SendClientMessage(playerid, color, const message[]);
Everything you need to change is color to the colour you defined and the const message[] to 'Welcome to my server'.

Step 5. You now need to include the parameters on SendClientMessage and add edit them.

pawn Code:
public OnPlayerConnect(playerid)
{
  SendClientMessage(playerid, COLOR_YELLOWGREEN, "Welcome to my server! Hope you enjoy your stay around here!");
Step 6. Now, since we want the message to be sent to the player, we need to add a return. Since we want it to be sent, the return as to be 1. So this is our final code:

pawn Code:
public OnPlayerConnect(playerid)
{
  SendClientMessage(playerid, COLOR_GREENYELLOW, "Welcome to my server! Hope you enjoy your stay around here!");
  return 1;
}


__________________________________________
__________________________________________
__________________________________________
If you want to include the player's name in the message, then follow this tutorial. Delete the above If you want to include the player name.

Step 1. You must define a color. It's been explained above. Ontop of the script, we'll need to introduce we wish the message to be. In this case, I'll be using GOLD, but you can choose your own color here. For that, we'll need to define the color. I'll show you how to define a color:

pawn Code:
#define COLOR_GOLD 0xB8860BAA
NOTE: If you're using another color, keep #define and change the rest with the desired color.

Step 2. You must include public OnPlayerConnect(playerid), because the message will be sent to the player as he joins the server.

pawn Code:
public OnPlayerConnect(playerid)
Step 3. Then we will need to create a 'string'. I suppose you already know how to make a string. If you don't, click here. This string is called pname - this string will get the player name. The size of that string has to be the max. characters a player's name can have, so we need to add MAX_PLAYER_NAME (24 is the default has added in a_samp.inc.

pawn Code:
public OnPlayerConnect(playerid)
{
    new pname[MAX_PLAYER_NAME]
Step 4. Now we need to create a new array for the welcome message. The size of the string depends on the ammount of text you want in the message. My message will be "Welcome to my server, PLAYERNAMEHERE.". We need to count the ammount of characters of the message. So I need to count the ammount of characters in the 'Welcome to my server' sentence - spaces, commas and dots included.

The ammount of characters is 23 (the dot at the end is included). So not let's include that array in our little 'script'.

pawn Code:
public OnPlayerConnect(playerid)
{
    new pname[MAX_PLAYER_NAME], string[23 + MAX_PLAYER_NAME+1];
NOTE: If your message is shorter/longer than 23 characters, you simply need to change the number '23' to the ammount of characters your message has.

Step 5. Now we need to get the player's name. For that, we'll use the function GetPlayerName. The player's name will be stored in the array pname. It's max length is sizeof(pName) - 24.

pawn Code:
public OnPlayerConnect(playerid)
{
    new pname[MAX_PLAYER_NAME], string[23 + MAX_PLAYER_NAME+1];
GetPlayerName(playerid, pname, sizeof(pname));
Step 6. Now that we know everything we needed, we need to create the message with 'format'. A 'format' "formats a string to include variables and other strings inside it." There a bunch of parameters that 'format' must have:
  1. output[] The string to output the result to
  2. len The maximum length output can contain
  3. format[] The format string
  4. {Float,_}:... Indefinite number of arguments of any tag
We need to use %s because it inserts a string. In my case, this is my 'format':

pawn Code:
format(string, sizeof(string), "Welcome to my server, %s.", pname);
Now we need to add that to our main code.

pawn Code:
public OnPlayerConnect(playerid)
{
    new pname[MAX_PLAYER_NAME], string[23 + MAX_PLAYER_NAME+1];
GetPlayerName(playerid, pname, sizeof(pname));
format(string, sizeof(string), "Welcome to my server, %s.", pname);
Step 7. We're reaching the final steps to make a successful welcome message. Now, we need to send the message to the player, therefore we need to use SendClientMessage. That function has several parameters.
  1. playerid The playerid you would like to display the message for.
  2. color The color it should be in.
  3. const message[] The text you would like to display. max len displayable: 144
Since we've already defined a message in a string, we're going to use it instead of typing a message. So, this is what we need to edit:

pawn Code:
SendClientMessage(playerid, COLOR_GOLD, string);
Now we simply add that to our code:

pawn Code:
public OnPlayerConnect(playerid)
{
    new pname[MAX_PLAYER_NAME], string[23 + MAX_PLAYER_NAME+1];
GetPlayerName(playerid, pname, sizeof(pname));
format(string, sizeof(string), "Welcome to my server, %s.", pname);
SendClientMessage(playerid, COLOR_GOLD, string);
Step 8. Last but not the least, you need to add a return. Since the message is sent to him, you're gonna set the return to 1 and add it to our code. This is our final code:

pawn Code:
public OnPlayerConnect(playerid)
{
    new pname[MAX_PLAYER_NAME], string[23 + MAX_PLAYER_NAME+1];
    GetPlayerName(playerid, pname, sizeof(pname));
    format(string, sizeof(string), "Welcome to my server, %s.", pname);
    SendClientMessage(playerid, COLOR_GOLD, string);
    return 1;
}

NOTE: It is showing 'Player' because I was using samp_debug.exe and connected to my localhost.
Reply
#2

hey i was going to create a thread like that ! using string etc
Reply
#3

Well, I guess I was faster than you!
Reply
#4

Good , thanks for this great tutorial !
Nice explained !
Reply
#5

Thanks, Ady_Ro. Means a lot!
Reply
#6

Just a heads up, if the player has 24 characters then last character gets cut off. Use MAX_PLAYER_NAME+1 ( https://sampwiki.blast.hk/wiki/GetPlayerName )
Reply
#7

That is pretty ok, but i would like to know how to script that a player can make their own welcome messages like : windows 8 user Hessu Joines the server. the command would be /welcomemessage.
Thanks!
Reply
#8

Good one for Beginners Try to make a Welcome message in Dialog and in Textdraw Example.


Reply
#9

Well explained tutorial, going to test this out
Reply
#10

Thank you very much, Andreas1331.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)