Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Should I use GetService when defining players?

Hi I am currently wanting to know if I should use GetService when defining players.I am experimenting with a few scripts at the moment and I’m currently defining players.I wanted to know if it’s necessary to use GetService when defining players.

Here’s my code:

local players = game:GetService(“Players”)

2 comments

1

Not necessary, but recommended.

1

Thanks ^

1 answer

✓ Accepted answer

#Intro

The GetService method is a method from ServiceProvider. The DataModel class (also known as game after the reference to 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 don't exist right away, and no you cannot do game:GetService("LolService").)

#GetService Saved Scripts

Back when FilteringEnabled did not exist / wasn't widely used after introduction, there were exploits that could rename services, and when they did, scripts that normally referenced a service by game.Service (e.g. game.Lighting) would break, as an object named Lighting (in my example) would no longer exist. When scripts used GetService, it would end up saving the scripts, and would continue executing as GetService would return the service requested via ClassName. There are also some services that require being obtained via GetService, the UserInputService being an example. This isn't really the case anymore with FilteringEnabled being forced. Any name changes without Remotes from a client will no longer replicate, they will obviously only change for the client which renamed the service.

#Should I use GetService?

It is more of personal preference. It is not much to worry about with exploits now, but is recommended nonetheless to avoid any possible conflict.

This discussion is preserved as it was. New answers, comments, and votes are closed.