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?
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.
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)