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

Help With Player Table?

local players = {game.Players:GetChildren()}

Now, in case you can't tell, I'm new to tables. I want a table that will display the names of all the players in the game, however, my attempt at it above won't work. How do I fix this?

4 comments

0

local players = game.Players:GetChildren() because GetChildren returns a table

0

That doesn't get me the player names, it just gets me a really random string of numbers.

0

just do for i,v in pairs(game.Players:GetChildren()) do print(v.Name) end that is because v then equals the child, then when it reaches end, v will equal the next child of players

0

check my answer,i updated it

2 answers

No. To get the player table you would do

local players = game.Players:GetPlayers()

The GetPlayers() method will only return player incase something else besides a player ends up in Players.

The problem is this: while setting tables manually does use similar syntax as {thingie, thingie, thingie} (and so on), game.Players:GetChildren() is a function. This means it returns the {thingie, thingie, thingie} array, instead of being an object inside one. Also, it is good practice to use :GetPlayers() rather than :GetChildren(). So, your code should look similar to this:

local players = game.Players:GetPlayers()

--Do something with the players

Here is a side-by-side comparison to clear the differences:

local players = {game.Players:GetChildren()} -- Your code
local players = game.Players:GetPlayers() -- My code

If you want to kill all the players, for example, here is how to implement this:

local players = game.Players:GetPlayers()

for _, plr in ipairs(players) do
	plr.Character.Humanoid.Health = 0
end

You probably won't need to understand all of this, especially the _, part. (I don't understand why it's used anyway)

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