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

How do I go through a table to find someone's name?

Asked by 10 years ago

What I mean is, how would I find someone from the players section by using a table. So for example if the code takes all the names and puts them in a table, how would I use that table then to find them all?

            for _,v in pairs (game.Players:GetChildren()) do
                    table.insert(players, v.Name)
            end
            for _,v in pairs (game.Players:GetChildren()) do
                if v.Name == players then
                v.Status = "Nub"
                end
            end

3 answers

Log in to vote
2
Answered by 10 years ago

game.Players:GetPlayers() returns a table. You shouldn't use GetChildren, and you don't have to insert each table individually.

If you want to see if a player is in the game, you can use the FindFirstChild method of the Players service

Ad
Log in to vote
0
Answered by
yurhomi10 192
10 years ago

you could try getting the current players like this

players = game.Player:GetChildren()

for key, name in pairs(players) do
print(key, name)
end
Log in to vote
0
Answered by
Maxomega3 106
10 years ago

You're close, except there's 3 things you need. 1) You don't need lines 1-3. game.Players:GetChildren () is the only table you need. 2) You can rename v to anything (best to name it player, so you can see what's going on better) 3) I'm not sure Status is a property of Player.

Here's a revision:

for _,player in pairs (game.Players:GetChildren()) do
    if v.Name == player.Name then -- needed the .Name after player because otherwise you're comparing a name to an object
    v.Status = "Nub" -- okay...?
    end
end

Answer this question