Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

1 comment

0

Huh, you're right, that'd be the easier way. Thank you very much!

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

1 comment

0

Or, if a[i].Name == "Player1" then -- code

This discussion is preserved as it was. New answers, comments, and votes are closed.