What is exactly _G

Recently I saw quite some people use _G. But what is _G? Help would be appreciated, and can you state some examples of them if possible thank you.

1 Like

_G is a table shared across scripts.

For example:

Script A

_G.apples = 5
print(_G.apples) -- outputs 5

Script B

print(_G.apples) -- outputs 5

Personally I do not use _G, just preference, I use the shared keyword instead, you may look into that if you like.

Read more here:

4 Likes

So if i save a variable in _G i can use it anytime?

1 Like

Yes! It is basically a module script that is already loaded, and can be modified by any script (take into account filtering enabled of course)

So even from Script B, I could set apples to be 10, and Script A would reflect those changes.

1 Like

but how do u set it to _G tho and how do i remove it

I don’t understand the first part of your question, but to dispose of a _G variable, it’s just like any other variable:

local apples = 5
apples = nil
-- apples is now disposed of, the variable is removed and will be removed from memory

_G.apples = 10
_G.apples = nil
-- _G.apples is now disposed of, same thing as above
1 Like

oh ok thank you for your help.

1 Like