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

Why should you use GetService() alot when scripting?

Asked by 5 years ago

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?

2 answers

Log in to vote
1
Answered by 5 years ago

GetService is the canonical way to get a service. Please use it.

Intro

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

GetService Saved Scripts

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.

Which to use?

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.

0
Thank you. Thesquid13 301 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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!

Answer this question