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

How do I access the names in the table that's created?

Asked by 9 years ago

I am wondering how I manipulate the name in the table that's created in this script.

for i,v in pairs(game.Players:GetPlayers()) do print(v) end

As you can see here, it'd print out all the players, though lets say I am trying to find Player1 and if he is in the game, then I want to remove him. How would I go about doing this? All I can figure out how to do with 'i,v in pairs() do' is print tables.

2 answers

Log in to vote
2
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

You don't have to use a for loop. Just use the FindFirstChild method. This method searches for a child, by name, and returns that child if it exists. Otherwise it returns nil. This makes it useful when coupled with an if statement to check if something exists.

if game.Players:FindFirstChild("Player1") then
    game.Players.Player1:Kick()
end
0
Huh, you're right, that'd be the easier way. Thank you very much! megamario640 50 — 9y
Ad
Log in to vote
-1
Answered by 9 years ago

I do not know how to use i,v in pairs, but this is my idea:

local a = game.Players:GetChildren()

for i = 1,# do  
    print(a[i].Name)
    a[i]:Destroy()
end
0
Or, if a[i].Name == "Player1" then -- code TheDeadlyPanther 2460 — 9y

Answer this question