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

Difference between game.Players and game:GetService("Players")?

Asked by 4 years ago

So basically, I always see scripts varying the usage of game.Players & game:GetService("Players") and I was wondering if there was any difference between the two? In which scenario would you use one over the other? Is one better than the other, and should be used every time?

2 answers

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

What is game:GetService()?

game:GetService() is a function that checks if the service is existent. If it isn't it will create the service. It is the only given way to make a service.


Difference:

There is not really a difference between them unless you're using a service that isn't loaded by default. For example, UserInputService. If you used the conditional dot operator, it would error saying "UserInputService is not a valid member of DataModel". Also, if you renamed a service, game:GetService() would work if you put the ClassName of it in the argument. For the conditional dot operator, you'd need to write the renamed name of the service. This can confuse other people reading your script.


Performance Test (Workspace):

game:GetService() - 0.002068042755127

game.Workspace - 0.00067710876464844

workspace - 0.00094294548034668

Using game.Workspace is the best way to do it performance-wise.


Which to use?

I personally would always use game:GetService() while indexing services. You can use either, but make sure that you use game:GetService() on services that aren't loaded in by default.

0
Ohhh thanks lol, I never knew that It's basically like game:GetService('TweenService'), its not already in Game so it will make it? LUCATIVIOMAD2 54 — 4y
0
Yes, it will make it. Also, please accept my answer. youtubemasterWOW 2741 — 4y
0
you don't need to use GetService() on workspace, 1. Its a global variable 2. Its a property of DataModel Luka_Gaming07 534 — 4y
0
I know. youtubemasterWOW 2741 — 4y
View all comments (4 more)
0
It also depends on if you change the name of the services pre-existing by default. For example, if you change ServerScriptService to ScriptingService or something else, you wouldn't be able to use game.ServerScriptService, you would be able to use game.ScriptingService or game:GetService("ServerScriptService"). killerbrenden 1537 — 4y
0
Thanks! matiss112233 258 — 4y
0
(2/08/2021) It wouldn't actually make it, it will get the service of the name e.g. TweenService. JesseSong 3916 — 3y
0
(15/08/2021) I'm still wondering how he did a perfomance test. I've tried everything JesseSong 3916 — 3y
Ad
Log in to vote
0
Answered by
o_fex 146
4 years ago

game.Players indexes a child of game named Players. If, for some reason, you renamed the Players service, game.Players will either index a nil value or, if a different child has the name Players, it will index that child. Thus, GetService() is considered the standard go-to for referencing services. GetService(serviceName) returns the service with the name serviceName. For every service (except the Workspace and any service that can’t be retrieved)

Answer this question