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

GetService("Players") or game.Players?

Asked by 8 years ago

I know how the GetService() method works, and I use it. But from looking at the wiki I noticed that that are children of game (i.e. Workspace, Players, Teams) all have a GetService():

game:GetService("Players")

Is there a difference between

local players = game.Players:GetPlayers()

and

local players = game:GetService("Players"):GetPlayers()

Is one more efficient? Are they exactly the same?

Another question similar to this, is

local players = game.Players:GetPlayers()

the same as

local players = game.Players:GetChildern()

or is one better than the other?

3 answers

Log in to vote
4
Answered by
Unclear 1776 Moderation Voter
8 years ago

The GetService method always returns a service given its class name. The significance of this is revealed when you think of the situations where this may be the only way to get a service. For example:

  1. If, hypothetically, a service is renamed.
  2. If the service cannot be indexed normally as a child of game (because it has not been created yet).

If you are creating code solely for yourself, you can get away with directly indexing from game (if it is appropriate for that service). However, if you are creating code that will be used by other people, you can save them a lot of headache if you use GetService by making sure your code works no matter what they do.

As for GetPlayers vs GetChildren, GetPlayers is useful for returning a table of players. The significance of this is revealed when you outline exactly what the two methods do:

  1. GetChildren returns a table of all of the children.
  2. GetPlayers returns a table of all of the players.

Notice how these two are not the same in all cases. For example, the Players service may have children that are not necessarily players, but will still be part of the array returned by GetChildren.

Ad
Log in to vote
2
Answered by 8 years ago

There is an unwritten rule about the GetService method being more reliable, though I don't know why. Just use that one for now until someone can provide a better answer.

Log in to vote
2
Answered by 8 years ago

@aquathorn321 is right. It's considered good practice, and is much more reliable between testing on single player, local servers and Roblox servers. It's not just the Players service, all servers should be requested in this way.

Answer this question