Avoiding :GetService() spam?

I was wondering if there is any easier and more compact way of handling game services to “GetService Stairs”

local RunService = game:GetService("RunService");
local TweenService = game:GetService("TweenService");
local ServerStorage = game:GetService("ServerStorage");
local PhysicsService = game:GetService("PhysicsService");
local ContentProvider = game:GetService("ContentProvider");
local CollectionService = game:GetService("CollectionService");
local ReplicatedStorage = game:GetService("ReplicatedStorage");
local ServerScriptService = game:GetService("ServerScriptService");
1 Like

If the Service is already in the Explorer (ServerScriptService,ServerStorage,RepicatedStorage) you don’t have to call game:GetService() for those since they already have been created.

If you want to make this more compact then you should just not save variables for each service. I think most people just do this to make their code easier to read.

3 Likes

Require a module which returns a table with all services for example.

local Serv = require(..)
Serv.Players.PlayerAdded:Connect( etc..
2 Likes
local realgame = game
local game = setmetatable({}, {__index = function(_, key)
    return realgame:FindFirstChild(key) or realgame:GetService(key)
end})

could maybe try something like this? prob some better way tbh

1 Like

I have a module with lots of the services and a few other common folders in it that I require at the top of every script.

local g = {}

--<>-- Variables --<>--

-- Services
g.repStor =				game:GetService("ReplicatedStorage")
g.lighting =			game:GetService("Lighting")
g.players =				game:GetService("Players")
g.userInputServ =		game:GetService("UserInputService")
g.contextActionServ =	game:GetService("ContextActionService")
g.starterGui =			game:GetService("StarterGui")
g.tweenServ =			game:GetService("TweenService")
g.runServ =				game:GetService("RunService")
g.soundServ =			game:GetService("SoundService")
g.httpServ =			game:GetService("HttpService")
g.collectionServ =		game:GetService("CollectionService")

-- Objects
g.camera =				workspace.CurrentCamera
g.terrain =				workspace.Terrain
g.events =				g.repStor.Events

g.repModules =			g.repStor.ReplicatedModules

--<>-- Server / client only --<>--

if g.runServ:IsServer() then	-- Server
	g.serverStor =		game:GetService("ServerStorage")
	g.serverModules =	g.serverStor.ServerModules
	g.serverEvents =	g.serverStor.ServerEvents
else							-- Client
	g.player =			g.players.LocalPlayer
	g.playerGui =		g.player:WaitForChild("PlayerGui")
	g.uiTemplates =		g.repStor:WaitForChild("UITemplates")
	g.uiEvents =		g.repStor:WaitForChild("UIEvents")
end

--<>--

return g

I paste this at the top of every script:

local g = require(game:GetService("ReplicatedStorage").ReplicatedModules.g)

Then if I want ReplicatedStorage for exapmle, I do g.repStor.

2 Likes

This won’t work for properties.

local services = setmetatable({ }, { __index = function(_, name)
    return game:GetService(name)
end })
print(services.Workspace)
1 Like

Lol I don’t really see the problem with typing them out?

2 Likes