Indicate Bad Service Practice in Script Editor

As a Roblox developer, it is currently not pointed out that directly indexing services is bad practice. game.Players experience.Experiencers, game.ReplicatedStorage experience.ReplicatedStorage, etc shouldn’t be indexed directly and showing editor warnings for these would be great for people learning Roblox scripting.

If Roblox is able to address this issue, it would improve the development experience because it would make it more clear to new users that they should use game:GetService experience:GetService.

16 Likes

I’d probably go a bit into detail as to why it’s bad practice namely:
a). GetService(String name) is a method apart of the ServiceProvider which is an abstract class that stores and provides singleton classes. It creates an instance of the singleton based on the name provided. If the service doesn’t exist, it’s created. This is the main fundamental differences and why this is the better route than calling game.Service directly.

b). If for some reason, you’re a madman and you rename your services, it won’t break. Good for team-related stuff where you’re transferring code between different games/test environments.

c). If it doesn’t exist yet and you reference it by game.Service, it’ll show up as nil plunging your code into a dark abyss and turmoil.

This would be nice for newer users.

I chose not to go into detail since it makes the request easier to read, I tend to make long requests which isn’t very good.

Reiterating/adding on, game:GetService("SomeService") experience:GetService("SomeService") is much better than game.SomeService for these reasons:

  1. If a service loads slow or stops being created by default for some reason it will completely break your code if you don’t use :GetService
  2. game:GetService experience:GetService relies on the class of the service, not the name. This is really useful because some services, like RunService are named differently than their class, so, it makes it more consistent and easier to look up on the devhub. And services getting renamed won’t break your code
  3. game:GetService experience:GetService auto completes in the script editor. You can see all services, and press Tab to auto complete them. This will speed up your scripting.
  4. It makes it easier to read. game.XXX experience.XXX isn’t clearly a service, but, game:GetService experience:GetService is.
  5. It might be more to type, but, generally this should always be done in conjunction with services. Not doing this is really laggy with or without game:GetService experience:GetService
-- PLEASE DISREGARD THE lack of USE OF THE experience VARIABLE
-- All of the services the code uses
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MarketplaceService = game:GetService("MarketplaceService")

-- The code

The benefits of doing this is that you type a lot less, and, since its all one word you can double click it to select, and copy paste it easily. Sometimes you change out ReplicatedStorage and ServerStorage as you move things around, so, this makes it really really fast to change them around. It also improves performance, using locals is faster than . or doing function calls because using . has to access the value every time, but, a local is just a quick reference to the value.

This is also why a lot of code always uses locals rather than . whenever possible. It’s way faster to do this for a lot of reasons and some situations.

1 Like