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

How do I find the name of a player, then use it?

Asked by 5 years ago

So heres my script.

function onClicked(player)

local nam = player.Name
if
game.Players.nam == true 

then
    game.Workspace:ClearAllChildren()

end
end


script.Parent.ClickDetector.MouseClick:connect(onClicked)

What I'm trying to do is find out if the clickers name, then cross examine it from the players list to make sure they're a player. How would I do this?

1 answer

Log in to vote
0
Answered by
Pojoto 329 Moderation Voter
5 years ago
Edited 5 years ago

You can say:

function onClicked(player)

    local nam = player.Name
    local found = game.Players:FindFirstChild(nam)

    if found then

        game.Workspace:ClearAllChildren()

    end
end


script.Parent.ClickDetector.MouseClick:connect(onClicked)

What we're doing here is that we're assigning a variable called "found" to the player's name in the game. We use FindFirstChild to find a child named "nam" in game.Players. If the player's name is not found, then the variable will be set to nil.

We check if it has been found by seeing if the variable is not nil:

if found then

This should work, however, you don't really need to be checking if a player exists because a player is automatically added to game.Players.

Ad

Answer this question