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

Is GetService better than game.Service??

Yes. GetService returns a service by its ClassName, and guarantees that that service will be returned. It should be used for every service. Workspace gets a pass, considering the ways to access it.

• game.Workspace (child of game)

• game.Workspace (property of game which holds an object reference to workspace)

• game:GetService("Workspace") (getting service)

• global workspace (workspace again but this method has been deprecated)

There are some services whose names are not their ClassNames. For example, the UserInputService.

local UIS = game.UserInputService -- Uh oh! We can't get it? :(

UIS = game:GetService"UserInputService" -- We got it! :D

We can rename these services, but we can't access them via game.Service anymore.

-- I manually renamed the Players service "Hamburgers". It is named "Players" by default.

local players = game.Players -- Where did Players go??
players = game.Hamburgers -- Hmph, here it is..??!!

players = game:GetService"Players" -- There you are! This GetService saved us.

3 comments

3

Did you make a question, and then answer it yourself? What?

0

Also, using just workspace to access the workspace is not deprecated, using Workspace with a capital w is deprecated.

0

^ Correct

1 answer

GetService will create the service if it does not exist. GetService and FindService are used in case the name has changed making the code more robust.

Both workspace and game.Workspace hold constant references to the Workspace if the name changes both methods will still return the workspace and since it does not need to be created we can use it without and issues. This only applys for this service and no other ones have constant references.

In short to make more robust code use GetService.

1 comment

0

I wonder if anyone ever uses FindService()

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