function getName() game.Players.PlayerAdded:Connect(function(player) local playerName = game.Players.Name return playerName end) end local d = getName() print(d)
Yeah I'm trying to practice returns and I suck at them.
It's a rather simple mistake, actually. When you want to print a name, you can instead use the argument passed by PlayerAdded
. So, you can replace line 3 with return player.Name
and remove line 4.
If it still doesn't work, you can use game.Loaded
, which is an event that fires on the client when the game client has completely loaded. (Bear in mind that this event can only be used in LocalScripts.) Specifically speaking, because this event only fires once, you can connect a function to it then embed your PlayerAdded
function inside of it. Better yet, you can use a RemoteEvent.
This event does not work as expected in solo mode, because the player is created before scripts that connect to PlayerAdded run. To handle this case, as well as cases in which the script is added into the game after a player enters, create an OnPlayerAdded function that you can call to handle a player’s entrance.
Try using onPlayerAdded.
https://developer.roblox.com/en-us/api-reference/event/Players/PlayerAdded