How to get a player's name?
#1

Hey guys, I wanna know/learn how to get a player's name for example, I have:

pawn Код:
SendClientMessage(playerid, 0, " A new player has joined the server ");
I want the server to show the player's name instead of saying " A new player ", Please!

Thanks a lot
Reply
#2

You use GetPlayerName and format.

Off-topic: That twodollarclick site is such a scam. They never pay out.
Don't know why people would waste their time on it. If it sounds too good to be true, it usually is.
Reply
#3

So. 1st you make a variable like new pName[MAX_PLAYER_NAME];

Max Player Name is how long the name can be (You can also put 24 or less but if the player name is longer than the value you put it will not show the whole name.

Now make a new variable. new string[256];

There's 256 because we do not need a longer string than a one with 256 cells.

Okay, so we give the pName variable a function: GetPlayerName(playerid,pName,sizeof(pName);

That means that pName will now show the player's name.

Now let us format the string: format(string,sizeof(string),"%s(%i) has joined the server",pName,playerid);

%s means that's a string, %i that's an integer(number).

Now let us send a message to the whole server someone joined:

SendClientMessageToAll(0xFFCC00,string);

That's all.
Reply
#4

Here is an example:
pawn Код:
public OnPlayerConnect(playerid)
{
    new name[MAX_PLAYER_NAME], string[28 + MAX_PLAYER_NAME];
    GetPlayerName(playerid, name, sizeof(name));
    format(string, sizeof(string), "( ! ) %s has joined the server",name);
    SendClientMessageToAll(COLOR_YELLOW, string);
    return 1;
}
where the "string[28 + MAX_PLAYER_NAME]" creates a new variable with the size of 28 + the maximum length of the player's name. (28+24=52). The reason why it should be 28 is because the "( ! ) %s has joined the server" is 28 carachters long (without the %s) where %s has the size of MAX_PLAYER_NAME (thats why they have to be +'ed together).

Then the players name is read in this line: "GetPlayerName(playerid, name, sizeof(name));"
which means the variable "name" will hold the player's name, and to hold the same amount of characters as "name" (sizeof(name)) which is MAX_PLAYER_NAME (24)"

And in this line we format the string: "format(string, sizeof(string), "( ! ) %s has joined the server",name);"
to the size of "string" which is 28+MAX_PLAYER_NAME (28+24=52), where "%s" is placeholder for the variable called "name" which is holding the name of the player and has a size of 24.

At the end we add ",name" to indicate that "%s" is placeholder for the variable called "name".

Now we just need to send our formatted string as a client message to all the players on the server. We do that by:
"SendClientMessageToAll(COLOR_YELLOW, string);"

Remember that "COLOR_YELLOW" is just a define for the HEX color "0xFFFF00FF".
You can du this by adding this at the top of your script:
pawn Код:
#define COLOR_YELLOW 0xFFFF00FF
Hope this didnt confuse too much.
Good luck learning scripting.
Reply
#5

Quote:
Originally Posted by Berlovan
Посмотреть сообщение
So. 1st you make a variable like new pName[MAX_PLAYER_NAME];

Max Player Name is how long the name can be (You can also put 24 or less but if the player name is longer than the value you put it will not show the whole name.

Now make a new variable. new string[256];

There's 256 because we do not need a longer string than a one with 256 cells.

Okay, so we give the pName variable a function: GetPlayerName(playerid,pName,sizeof(pName);

That means that pName will now show the player's name.

Now let us format the string: format(string,sizeof(string),"%s(%i) has joined the server",pName,playerid);

%s means that's a string, %i that's an integer(number).

Now let us send a message to the whole server someone joined:

SendClientMessageToAll(0xFFCC00,string);

That's all.
256 is way too big for such a simple message, and will only cause more lagg on the server.
Reply
#6

Quote:
Originally Posted by sim_sima
Посмотреть сообщение
Here is an example:
pawn Код:
public OnPlayerConnect(playerid)
{
    new name[MAX_PLAYER_NAME], string[28 + MAX_PLAYER_NAME];
    GetPlayerName(playerid, name, sizeof(name));
    format(string, sizeof(string), "( ! ) %s has joined the server",name);
    SendClientMessageToAll(COLOR_YELLOW, string);
    return 1;
}
where the "string[28 + MAX_PLAYER_NAME]" creates a new variable with the size of 28 + the maximum length of the player's name. (28+24=52). The reason why it should be 28 is because the "( ! ) %s has joined the server" is 28 carachters long (without the %s) where %s has the size of MAX_PLAYER_NAME (thats why they have to be +'ed together).
Then the players name is read in this line: "GetPlayerName(playerid, name, sizeof(name));"
which means the variable "name" will hold the player's name, and to hold the same amount of characters as "name" (sizeof(name)) which is MAX_PLAYER_NAME (24)"
And in this line we format the string: "format(string, sizeof(string), "( ! ) %s has joined the server",name);"
to the size of "string" which is 28+MAX_PLAYER_NAME (28+24=52), where "%s" is placeholder for the variable called "name" which is holding the name of the player and has a size of 24.
At the end we add ",name" to indicate that "%s" is placeholder for thevariable called "name".

Hope this didnt confuse too much.
Good luck learning scripting.
Actually, it would be 29 cells plus "MAX_PLAYER_NAME" due to the null terminator.
Reply
#7

Thank you guys, I will try now but what does that number mean in new string[256]; ? What does it effect and what is it, please tell me more about the String[number]
Reply
#8

Quote:
Originally Posted by Ehab1911
Посмотреть сообщение
Thank you guys, I will try now but what does that number mean in new string[256]; ? What does it effect and what is it, please tell me more about the String[number]
Look at my post, i described what it means. The [number] is called an "array".
An array is a variable in which you can store multiple pieces of data at once and access them dynamically. An array is declared to a set size at compile time so you need to know how many pieces of data you need to store in advance, a good example of this is the very common MAX_PLAYERS array, this will have one slot for every possibly connected player, so you know data for one player will not interfere with data for another player.

And dont use 256, its way too much, and will only cause the server to lagg.

*EDIT*
Lol Ehab, didnt notice it was you (again)
Reply
#9

Sim_Sima, thank you for your FREACKING USELESS POST. You posted 5 FREACKING MINUTES AFTER I DID + Your 1st message was only THE EXAMPLE. You freacking edited your message 3 times. And @Ehab, The string[number] means how much cells (numbers,letters,spaces) we need in the string. Like this : "hello", This string requires 5 cells. If we would pu : "Hello there" it would require 11 cells. Size of the "Hello" + " "(space) + size of "there".
Reply
#10

Quote:
Originally Posted by sim_sima
Посмотреть сообщение
Look at my post, i described what it means. The [number] is called an "array".
An array is a variable in which you can store multiple pieces of data at once and access them dynamically. An array is declared to a set size at compile time so you need to know how many pieces of data you need to store in advance, a good example of this is the very common MAX_PLAYERS array, this will have one slot for every possibly connected player, so you know data for one player will not interfere with data for another player.

And dont use 256, its way too much, and will only cause the server to lagg.

*EDIT*
Lol Ehab, didnt notice it was you (again)
Lol np Sima =] Thanks alot, but I will read it in a sec ^^ <3
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)