You can either write:
workspace:WaitForChild("Part")
or
game:GetService("Workspace"):WaitForChild("Part")
I see many people choose the second one, what does getservice actually do and why should it be used?
GetService is the canonical way to get a service. Please use it.
The GetService
method is a method from ServiceProvider. The DataModel class (also known as game
after the global variable that references it it) inherits from ServiceProvider and therefore inherits this method. This method would return the service you are requesting by ClassName
, creates service if it does not exist (some services do not exist right away, and no you cannot create custom services).
Back when FilteringEnabled
did not exist/wasn't widely used even after introduction, there were exploits that could rename services, and when they did, scripts that normally referenced a service by game.Service
would break, as an object named Lighting (in my example) would no longer exist. When scripts used GetService
, it would not break, and would continue executing as GetService
gets a service via ClassName
. There are also some services that require being obtained via GetService
, the UserInputService being an example. I especially encourage you use GetService
on local scripts since clients can rename services and thus break your scripts that index the DataModel directly.
However you don't need to worry about clients renaming your services and breaking your server scripts since FE is now forced and the changes won't even replicate.
GetService
is recommended. Also if you plan on making scripts for other people to use and they for some reason rename their services your scripts won't break on them. And it will help you on the long run too.
GetService Get means to get something, in this case, a service. Which service? Roblox Service. Roblox Service example is StarterGui
game:GetService("StarterGui") or :GetService('StarterGui')
A service is very important. ???????? Try your best!