Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Get A Player's Name?

I can't seem to get this script to work:

game.Players.PlayerAdded:connect(function(playerAdded)
print(player.Name)
end)

The goal of the script is to print the player's name once they enter the game. Can anyone help?

3 answers

✓ Accepted answer

You only made one mistake. That what is in between the parenthesis after the function keyword, are the parameters. The value returned from the PlayerAdded event is the/a player. This data is stored in the parameter. The parameter essentially acts like a variable and it value is received from the event in this case.

The parameter - or variable, if you would prefer that - playerAdded contains the player. To fix your script, there are two options:

  • Change player.Name to playerAdded.Name. This makes the code look like this:
game.Players.PlayerAdded:connect(function(playerAdded)
print(playerAdded.Name)
end)
  • Or change playerAdded to player. This makes the code look like this:
game.Players.PlayerAdded:connect(function(player)
print(player.Name)
end)
game.Players.PlayerAdded:connect(function(player) -- This is the player. The PlayerAdded event knows who the player is.
	print(player.Name) -- Printing the player name.
end)

When you have the event PlayerAdded, the function's parameters will always be the player.

Make sure it is in a LocalScript.

1 comment

0

Once again I am being down-voted for trying to help...

This discussion is preserved as it was. New answers, comments, and votes are closed.