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

Is doing :GetService better then just doing .'s?

Asked by 5 years ago

So I am trying to get a remote event but I am wondering if any of these techniques get service or just using . to get a remote event from a replicated storage is slightly better by that I mean performance and how it is gotten everything

For example Game:getService(ReplicatedStorage):WaitForChild('RemoteEvent')

or just doing game.replicatedstorage:WaitForChild('RemoteEvent')

which one is better to use or are they all the same

0
game.ReplicatedStorage because it's already a service within the game by default. GameBoyOtaku 63 — 5y
0
Its usually thought that using :GetService is better but this is usually if your giving your scripts to someone else, who may rename some of the services. Wafflecow321 457 — 5y

2 answers

Log in to vote
2
Answered by 5 years ago
Edited 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.

Ad
Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

They’re generally both identical, though :GetService() is a method predominately built for referencing primary Services. Everything that’s a Child of game is a primary Service so when index them it’s preferred to use this method. It also allows access to other second-party Services such a DataStoreService, MarketplaceService, further on.

. is for stepping through structural hierarchys, this is usually for ascending downwards in Objects. Also allows you to access the Objects properties of properly formatted

Now knowing the difference, it’s preferred that you use . with ReplicatedStorage to get the Remote, but for ReplicatedStorage you should use GetService. Generally these are primarily opinion based uses, though '.' is more diverse than GetService, so remember that.

Answer this question