So I have lately been seeing a lot of people user Services to get things inside of the game I was wondering why its necessary an example of what I am talking about is something like this...
Local players = game:GetService("Players")
Why do they use the one above?
Local players = game.Players
Why not use this one above? Does it matter is there a difference between those two? Isn't it getting the same thing? Please I would love it someone would tell me which one is more efficient and smoother to use programming wise.
Even though it's not a good idea, most services can be renamed.
That means that if you're writing code that you want other people to use, and you don't want it to get broken by a renamed service, you use :GetService
, which searches by the service's classname instead of the name the developer put in Studio.
In this way, it's a little more robust. Usually you don't want lots of pieces of code depending on lots of minor configuration details. Though it's not necessarily a good idea to rename ServerScriptService to SSS, you'd probably not want that change to break everything.
Some services don't exist inside game by default, and you can't create them via Instance.new
. To create these services for your use, you call GetService
. If the service doesn't exist, GetService
creates it for you. because ROBLOX holds the right to make any service except for the Workspace act like this, you should use GetService
wherever possible.