Is there an advantage to using :GetService()?

So I was wondering. Is there a difference between
game:GetService("ReplicatedStorage")
and
local ReplicatedStorage = game.ReplicatedStorage
If there is a difference, should I use one over the other?

6 Likes

Im pretty sure if the replicatedstorage doesnt load in time then that script will break. And by doing game:GetService(“ReplicatedStorage”) its essentially waiting for it to load.

1 Like

GetService does a classname look up and ensures you get the service you asked for. Using the dot operator gives an object by name, not by classname, so if you rename ReplicatedStorage to RepStore to give an example, the dot operator will error, if you do game.ReplicatedStorage, but GetService will not as the classname will stay the same regardless of name. The only exception is game.Workspace, because it is a property for some reason.

GetService is usually the more used approach to ensure that it will never error even if you or someone else using your script renames their services unless a service gets removed

Edit: Also, quickly reading the docs for GetService, if the server does not exist by the time GetService was used, it gets created and returns the newly created service

4 Likes

While dot notation is faster, :GetService() is safer. If a service doesn’t yet exist for whatever reason then :GetService() will create that service and return it. It’s also cleaner for some services who have whitespaces in their names such as Run Service. If you attempt to use dot notation to get Run Service you have to use brackets:

game['Run Service']
-- vs.
game:GetService('RunService')

But once again the primary reason for using :GetService() is that it is far safer than trying to directly reference a service.

7 Likes

either way you should use “workspace” instead of that

1 Like

Yes I know that lol, I mentioned it as it is the only exception to the dot notation for retrieving services, even if you rename the workspace service, you’ll never get any error because it’s a property that contains the Workspace service in it. So all these

game:GetService("Workspace")
game.Workspace
workspace

Are the same in terms of safeness so it doesn’t matter what people use, if people want consistency they could use

local Workspace = game:GetService("Workspace")

like they would do for every other service. The other two would stick out like a sore thumb. Yes sure workspace is shorter and I use it as well since I’m okay with the inconsistency, but there may be others who aren’t okay with it and would use the exanple I mentioned, there’s no right or wrong for retrieving the Workspace service in this case

2 Likes

That is incorrect, :GetService() — if the service does not yet exist it will be created and the new service is returned.

More information can be found here: ServiceProvider | Roblox Creator Documentation

1 Like

So I ran a quick benchmark and it appears :GetService() is faster than using the dot operator:

local x = os.clock(); for i = 1, 1000000, 1 do local y = game.Players end; print(os.clock()-x)
-- ~0.7

Versus:

local x = os.clock(); for i = 1, 1000000, 1 do local y = game:GetService("Players") end; print(os.clock()-x)
-- ~0.4

So :GetService() is safer, more recommended, and faster as a bonus. Only a small extra amount of typing.