Why Does Roblox Not Have Service Globals?

Why does Roblox not have service globals?

game:GetService(“Service”) or game.Service is probably one of the most rewritten pieces of code in scripting that slows down workflow and clogs up scripting space to declare their variables. Not only that, but scripts will usually have a different name for these like:

local run = game:GetService("RunService")
local RunService = game:GetService("RunService")
local runS = game:GetService("RunService")
local RS = game:GetService("RunService")
local rs = game:service("RunService")

This leads to inconsistency when reading code.

ModuleScripts

You could use a ModuleScript containing these services, but that prevents scripts from being stand-alone. They will need to have a reference to the module script. If they were shared or posted on a debugging forum, then these scripts would not work and people would have to waste time rewriting or adding these services to the script. Then, the original owner would have to take the edited code and replace the new service variables with their own.

service Global

Why not add a service global to Roblox that holds services? This would vastly improve readability in code and speed up workflow.

By having a service global, this would further improve readability by highlighting them blue in the script editor to help users realize when a Roblox Service is in use. service would also demotivate people from using the deprecated :service() function for new work. Services would no longer need to be declared in every script which will free up script real estate.

The workspace global would also become consistent with Service capitalization so people who want consistency would use service.Workspace instead of game:GetService("Workspace"). The inconsistency of using local Workspace = game:GetService("Workspace") instead of workspace has confused users before.

Lastly, having a service global would reduce the number of people who use the bad practice of indexing services in code like game.Players. This would reduce headaches for people that wonder why everything is broken when they change the service name and allow Roblox to freely change the name to modern standards.

for _, v in next, service.Players:GetPlayers() do
	-- Code
end
-- The old workspace global is now consistent with Service Capitalization
local character = service.Workspace.Characters:FindFirstChild("Shedletsky")

service.RunService:BindToRenderStep("Render Ocean", Enum.RenderPriority.Camera.Value - 6, updateOcean)
2 Likes

Sorry if this looks like I posted in the wrong category. I can’t post in #platform-feedback:engine-features and forums that said to use the “post approval procedure” kept redirecting to nonexistent content. It started as a general question that kept evolving.

That is what game is. game is of class DataModel, and therefore also a ServiceProvider.

The reason why using game:GetService() is supposed to be used is because even though you can technically use game.AdService, it’s not actually loaded since it’s use is uncommon. When you call :GetService(), it loads the service into your game in the background for you to use.

You can call :GetService() if it’s not found in the service global using the __index metamethod. That way, it won’t actually load any service until it is called upon.

-- ModuleScript called "Service" in ReplicatedStorage
local service = {}

setmetatable(service, {__index = function(_, name: string)
	service[name] = game:GetService(name)
	return service[name]
end})

return service

Then for other scripts:

local service = require(game:GetService("ReplicatedStorage"):WaitForChild("Service"))

for _, v in next, service.Players:GetPlayers() do
	print(v.Name)
end

This ModuleScript defeats the purpose of what I’m requesting, however, because I need to get ReplicatedStorage to ever even use this service module. Furthermore, the script would be restricted only to games that use this ModuleScript.