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?
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?
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.
4 comments
local players = game.Players:GetChildren() because GetChildren returns a table
That doesn't get me the player names, it just gets me a really random string of numbers.
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
check my answer,i updated it