Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago
Edited 4 years ago

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!

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. User#5423 17 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

0
Yeah, i could've thought of that HomieFirePGN 137 — 4y
Ad

Answer this question