Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

I'm trying to make it so it prints the player name but it won't work?

Asked by 5 years ago
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.

2 answers

Log in to vote
0
Answered by 5 years ago

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.

Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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

0
Well, your answer makes no sense. You say that PlayerAdded will not work and yet you tell the asker to use a function that uses PlayerAdded. DeceptiveCaster 3761 — 5y

Answer this question