Safe to use _G. variables for services?

Is it safe to store services as:

G_SS = game:GetService(“ServerStorage”)

So i can easily access services from anywhere instead of making variables for practically every script?

Theres nothing harmful from this but I don’t see much point and personally I avoid using _G

local ServerStorage = game:GetService('ServerStorage')
local ServerStorage = _G.SS

Not much better tbh and if another script decides to use _G for another reason it can be irritating.

Being honest, there is no real point, few reasons being:

A - This would involve a system that ensures the globals load first. If you have a script calling that global, but the global hasn’t yet been initialized, it would error, as well the script calling the global wouldn’t be able to find the global as it hasn’t yet been registered.

B - It’s easier to just have the services in the script, more readability and saves you time having to go to the main script to see each service (depending on the variable names)

If the exploiter knows what they’re doing, they can probably replace the variable with an intermediary and intercept actions. Server should be safe though (meaning your sample is fine).

Don’t store variable in _G.

However you should store :GetService calls at the top of your script.

Like

local ServerStorage = game:GetService("ServerStorage")

Do not use shorten name use the full name, shorter names aren’t better in any way.

Shorter names are easier to type and less vulnerable to spelling mistakes (Ignoring the Auto-fill), especially if you abbreviate the names of the services. It won’t hinder readability if you know what you’re doing. Also, a shorter name obviously means less data is used for storing the script.

Some common abbreviations that I use:

local res = game:GetService('ReplicatedStorage') --(RE)plicated(S)torage
local rus = game:GetService('RunService') --(RU)n(S)ervice
local sst = game:GetService('ServerStorage') --(S)erver(ST)orage
local sss = game:GetService('ServerScriptService') --(S)erver(S)cript(S)ervice
local tws = game:GetService('TweenService') --(TW)een(S)ervice

If you still want to create an easy access point to services among all scripts, you can try creating a ModuleScript storing the said services and require it at the top of each script.

1 Like