Game.players:getplayers() or game.players:getchildren()

Why should anybody use getplayers from my understand it’s practically the same functionality wise as say game.player:getchildren() they both do the same thing. Honestly I don’t even see a case where getplayers() is essiantial it seems like a getchildren reskin except extremely niche as hell. Plenty of scripts in toolbox’s I’ve seen avoid getplayers() even in ipairs/pairs their has to be more to this built in function

VISUALLY

Players (service)
Player1
Player2
Player3
Player4

game.players:getplayers()[1] — first player—
game.players:getchildren()[1] — first player —

getplayers() gets players one through 4
Getchildren() gets players one through 4

The annoying thing is I use both but typically lean towards getchildren() all in all their isn’t really alot of cases when it can be used a lot of other keywords have multi uses for code in way getplayers just seems like it does as it’s name implies

If you don’t really want a life story in shorts I want to know when getplayers() is a optimal and right choice well scripting cause if I am going to use it I want it to be properly used. Does getplayers do anything other then it’s direct name sake??

1 Like

This method returns a table of all presently connected Player. It functions the same way Instance:GetChildren() would except that it only returns Player objects. It functions similarly to Instance:GetChildren() when called on Players. When used in conjunction with a for-loop, it is useful for iterating over all players in a game.
(Players | Documentation - Roblox Creator Hub)

I’d agree with your perspective, but it’s safer to use :GetPlayers() just due to the fact that it only returns actual Player instances, whereas with :GetChildren() it’s possible (though very unlikely) that you could encounter non-player objects. If you do use :GetChildren(), you also have to filter out non-player instances on the off-chance you could encounter them.

Players:GetPlayers() is safer to use in these instances, and if you really don’t want to use it: Don’t. On the other hand :GetPlayers() increases code readability considering it is an extremely common function.

Pros for Players:GetPlayers(): safer to use so you don’t encounter unintended instances, better code readability
Cons for Players:GetPlayers(): None.

Cons for Players:GetChildren(): You have to check that the instances you’re using are player instances, adding another line of code to your scripts. Not so fun.

Hope this sums it up. They’re close the the same, but I’d recommend using Players:GetPlayers().

It may be extremely niche, but if you use :GetChildren(), you will always run the risk of encountering a non-player object.

2 Likes

It’s possible for non player objects to to download acsess to the games client?? I cannot see this ever happening unless you go out of your way to store or add parts in player service via instance.new or pre-existing

I’ll take your advice though and try to use getplayers when coding with this service as in some cases I’ve noticed it doesn’t effect performance negatively as much

Also besides pairs loop and getting players in the game the keyword has no other hidden or valid uses?

1 Like

It’s very unlikely but there is always a small chance, that’s why it’s safer and saves time to use :GetPlayers() as you don’t have to filter out instances that aren’t players. And to answer your question I don’t believe it has any other uses than getting player instances.

1 Like

That’s not what I’m saying when I reference non-player objects. I refer to instances that could be placed within the Players container during runtime. I agree with you that this would be a really rare instance, and you’d have to do it yourself, but it’s still nicer for your code to use :GetPlayers() due to the ease of use and the fact that you don’t need to type check compared to using :GetChildren().

When you talk about performance, I’d say that it wouldn’t and shouldn’t effect your performance at all, because they’re practically the same thing.

And no, they don’t really have any other valid uses besides what it says in the name.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.