Both of those achieve the same thing. GetChildren gets everything inside the place where you want. GetPlayers only gets the Players. There are only player objects in game.Players so GetChildren will also get all the players.
Yeah, but why is there two identical functions that return the same thing?
They don’t return the same thing. GetChildren returns a table with everything in a specific location. GetPlayers returns a table with only players.
GetPlayers()
returns player objects in the Player’s service while GetChildren()
get’s everything, even non player objects in the Player’s service if found.
There are non-Players object in the Player’s Service? What. In what case would you have non-player objects there. Just curious since I never use it.
There aren’t. If there are, :GetChildren()
will return them as well. I did mentioned ‘if found’.
for _, v in ipairs(game.Players:GetChildren()) do
print(v) -- print everything inside the players service
end
for _, v in ipairs(game.Players:GetPlayers()) do
print(v) -- will only print if a player object found
end
There should not be any non-player objects in the Player’s service.
Say a script parents something to Players
, :GetChildren()
will find that child. :GetPlayers()
only gets the players, which means it will not find that specified child the script added to Players.
So both ways get any child of the service. I understand that one Gets player objects, whilst the other gets every child. Since they both get the same thing, is there any differences?
For example
--Players-- Get every child inside
Player1
Player2
--Players-- Gets all player objects inside
Player1
Player2
You can see that they both get players. There is no difference, except for checking whether or not player object exist or not. They both get the players regardless.
Player’s service will only have players, but by chance people can insert something inside the Players service. :GetChildren()
will return everything inside it while :GetPlayers()
will return only players.
Of course :GetChildren()
will return the players because there are players inserted to the Player’s service, if there were for e.g a CFrame object, :GetChildren()
will also return the CFrame object.
That’s also the reason why :GetPlayers()
is better.
Is there any reason why someone would insert objects into the service? I mean…
No, but people can. Player’s service isn’t a place for storing stuff but you never know if a person is going to insert or not.
Thankyou everyone for clarifying this. I always use GetPlayers under the Player’s Service, instead of the other, obviously.
I just don’t see the use for getchildren over getplayers under the service. I understand the functions and what they get, but they both still return players under the service. Since, players should be the only thing to exist, regardless. As @ArrantNickel mentioned you can create objects to store under the player and get that using GetPlayers, but that’s really the only reason why. I don’t see any other uses.
And hardly anyone stores objects under the service.
GetPlayers only return all player inside player service.
While GetChildren return everything on player service, include instance, model, or etc.
By default they wouldn’t and shouldn’t be any instances, models or ect. You can see what I mean.
The main difference is that :GetChildren() will return players in the game as well as players that are not in the game while :GetPlayers() only returns players that are still in the game. This is useful if you want to know how many players are in the game after someone leaves.
Some anti-cheat devs will throw things into the players service to throw off exploiters
Instance:GetChildren() is a function that nearly everything inherits. Players:GetPlayers() was written for cleanliness and possible edge-cases. In most cases, they would do the same thing, but you should use :GetPlayers() because it was specifically made for this scenario.
Why did you revive this? This has been settled for a long time… I didn’t see that the last post was over a year ago until I had already answered. Things archive for a reason.
There is a difference. If you are using game.Players:GetChildren()
in a script, it will return with all variables within Players. Hence why we use game.Players:GetPlayers
, because it will only return Player objects within Players. The only real time you should use game.Players:GetChildren()
is when you have a loop, otherwise never use it. This is because for loops go through every variable within the pairs
or ipairs
part of them.
i
= Index, or the order it is going in/ the current value.
v
= Variable, or which object the index is on.
The only time you should ever use game.Players:GetChildren()
is when you want EVERY child of the Players class. Otherwise, stick to game.Players:GetPlayers()
.