_G help! What does it do?

So I am working on a new RPG game and I decided that I am going to use module scripts so I can keep everything Organised and easy to locate if there is an error.

I have one main module called GameSetup and this is going to be the parent of many module scripts.

I do have a question and that is what is _G mainly used for? as of right now all I know its a global variable that can be used across different scripts. I have seen this somewhere:

image
and I’m not quite sure what this is actually doing? I may sound stupid because this might be a easy thing to understand, but i’m not sure what it does.

1 Like

_G is just a global thing that, before modulescripts, was often used like a modulescript:

--In some script:
_G.Module = {}

_G.Module.DoThing = function(Thing)
    print(Thing)
end
--In some other script:
_G.Module.DoThing("Hello World")

The advice now is to use modulescripts, as they are newer and have better performance.
If you need to share something with a module like you would with an _G variable, you can just do this, and require the module from any script that needs that value.

module.GetValue = function()
    return Value
end
2 Likes

You don’t use modules over _G because of better performance, in fact there really isn’t any performance benefit.

stravant’s post here explains excellently why modules > _G:

2 Likes