SA-MP Forums

SA-MP Forums (https://forum.sa-mp.com/index.php)
-   Tutorials (https://forum.sa-mp.com/forumdisplay.php?f=70)
-   -   [Tutorial] 'Welcome to my server' messages. (https://forum.sa-mp.com/showthread.php?t=450809)

Twizted 14/07/2013 08:13 PM

'Welcome to my server' messages.
 
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;
}

http://gyazo.com/1027b1b866ddf2245f09166037343f70.png

__________________________________________
__________________________________________
__________________________________________

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;
}

http://gyazo.com/f47e58c44f436415123092437a130009.png
NOTE: It is showing 'Player' because I was using samp_debug.exe and connected to my localhost.

Chrisis 15/07/2013 04:45 PM

Re: 'Welcome to my server' messages.
 
hey i was going to create a thread like that ! using string etc

Twizted 15/07/2013 04:51 PM

Re: 'Welcome to my server' messages.
 
Well, I guess I was faster than you!

Ady_Ro 15/07/2013 05:26 PM

Re: 'Welcome to my server' messages.
 
Good , thanks for this great tutorial !
Nice explained !

Twizted 15/07/2013 05:35 PM

Re: 'Welcome to my server' messages.
 
Thanks, Ady_Ro. Means a lot!

Misiur 15/07/2013 06:23 PM

Re: 'Welcome to my server' messages.
 
Just a heads up, if the player has 24 characters then last character gets cut off. Use MAX_PLAYER_NAME+1 ( http://wiki.sa-mp.com/wiki/GetPlayerName )

Hessu 08/02/2014 10:02 AM

Re: 'Welcome to my server' messages.
 
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!

KaliKs 08/02/2014 09:25 PM

Re: 'Welcome to my server' messages.
 
Good one for Beginners Try to make a Welcome message in Dialog and in Textdraw Example.


http://colorhost.co/gamecp/tracker/?...=d45e14&ts=000

Andreas1331 01/05/2014 12:24 PM

Re: 'Welcome to my server' messages.
 
Well explained tutorial, going to test this out :)

Twizted 07/05/2014 01:44 PM

Re: 'Welcome to my server' messages.
 
Thank you very much, Andreas1331.


All times are GMT. The time now is 05:28 PM.

Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.