What is _G and for what i can use it?

I’ve got a question:

If you use _G in a localscript, will it still save to server scripts?

no, it’s specific to the session and device

I see, thanks for clarifying that.

Some developers say the use of _G is bad practise but in my opinion it is not. From my point of view, I recommend only using _G when it is absolutely necessary, and to use it to define the main game services, e.g. ReplicatedStorage, ServerScriptService, SeverStorage, etc. I use the main game services in almost all the scripts I write and making them global is a good idea, but other developers use ModuleScripts and a combination of both. I also don’t see Roblox deprecating _G anytime soon, either.

I reckon even in this case, its better to use a ModuleScript rather than _G. You still have to define each _G.[Service] at some point, and you still have to check that it exists before attempting to access it. If you use a ModuleScript instead, you keep all of that code in one place and only have to check that one module exists (as opposed to making a separate check for each service). This leaves you with less redundant and hard to parse code sitting in your scripts.
Part of the problem with using _G is that it’s accessible in every script, not just scripts that explicitly require it. It’s much easier to make changes to how your scripts behave if related code is grouped together, rather than spread across several scripts.

_G isnt safe because if your game is backdoored, they can use _G to really mess up your game. If you have datastores saved to _G, they can permanently write and read everything. They could even wipe it all with a click of a button.

For some reason, this prints nil for me.
Does anyone have any ideas?

script 1:

_G.hi = 'wow'

script 2:

wait() -- will wait until _G.hi is defined (which happens fast so we dont need to repeat
warn(_G.hi)
1 Like

then do task.wait() for a smaller delay

1 Like

The real answer is to not use _G at all so you don’t have to poll.

nothing is safe if they actually backdoor a game, this is the 2nd time I have seen you say this and I don’t get it

maybe you wont have this issue if your game doesnt get backdoored? just a thought?

ok but why are you saying this to me

Wrong person lol. My apologies, I hadnt noticed.

This is useful, thank you very much

Can I just ask. If you used _G.name = script.Parent.Name would it only be for the current script which used _G or would it change for every script you used it in: e.g. if you used _G in a script named “global” then you called that variable in another script would it still print “global” or print the name of that other script instead?

Only for other scripts of the same context level (server/local), think of _G as having a similar behavior to ModuleScripts.

--SERVER SCRIPT

_G.ScriptName = script.Name
--ANOTHER SERVER SCRIPT

task.wait(1)
print(_G.ScriptName) --Script
--LOCAL SCRIPT

task.wait(1)
print(_G.ScriptName) --nil

you can only use _G on the server unless an exploiter gets access to a remote event

Thoughts on using _G to fire a localscript that handles UIs using Signal? e.g

-- UI Script --
_G.UIs = {
    MainMenu = Signal.new()
}
-- Does what it needs to do when listening to Signal

-- Another Script --

_G.UIs.MainMenu:Fire()

It works if you can ensure the script order signal is created before the :Fire(),

Hence why module scripts are preferred because you can control script order through the require function.

This should be explained somewhere deep in this post already

1 Like