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

Which is better to use in a script, :GetPlayers() or :GetChildren()?

Hi, I saw this script in a book. Nothing was wrong with it except one thing didn't seem right. The author referenced players as:

local Players = game.Players:GetChildren()

when usually it is:

local Players = game.Players:GetPlayers()

I'm not sure whether it was best to use the first or second option.

Please help!

1 comment

0

This goes back to a more "old school roblox" where the events we had are very limited. So you did not even have PlayerAdded you would use ChildAdded. So this is the same with GetPlayers as this did not exist. You would get the children under the players service and expect them to be the player object.

1 answer

✓ Accepted answer

While both usually end generating the same array, they are two different things. GetChildren returns everything that's parented to that object, in this case, Players, whether it be a player or something else, while GetPlayers only returns players and nothing else.

Use GetPlayers if you purely want the players, and GetChildren if you only want all its children.

https://developer.roblox.com/api-reference/function/Players/GetPlayers

1 comment

0

Yeah, i could've thought of that

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