What is _G and for what i can use it?

My life has been changed. Thank you random stranger on devforum lol

2 Likes

Module scripts can contain variables or functions that can be accessed anywhere (by requiring them), just like with _G variables (note that one _G variable cannot be accessed from server to client or vice versa), the only difference is that the modifications you bring to a _G variable will actually be changed cross script.

That is what makes _G variables so useful. We all know it has its ups and downs, but if you play your cards right with the _G var it can be pretty damn useful.

I do, however, recommend that people use Module Scripts if they have never played with _G vars before. Years ago it was worth learning, but now it is not.

_G in my opinion is pretty bad, exploiters can do

for i, v in pairs(getrenv()._G) do
        print(i, v) -- I Is the name and v is the type 
end

Exploiters have their own environment (if im right) and getrenv() lets them access roblox’s environment (ur game environment)

Same with shared too like this:

for i,v in pairs(getrenv().shared) do
      print(i,v) -- Can also be tables, functions, etc 
end 

Plus, all the things the exploiters can do to your global variables (eg, if it’s a function they can ruin it by doing hookfunction to it and changing it to a custom function & same with variables, they can change it easily)

So it’s pretty bad when storing Important variables such as Money, Data, etc.

ModuleScripts also are pretty bad since they can require it and do a for loop to check all the stuff inside the ModuleScript table (or anything else you return in the ModuleScript)

    for i,v in pairs(require(modulepath)) do
          print(i, v) -- if im right, I is the index and v is the type.
    end
2 Likes

If thats what you are worried of, modules also won’t be of any help:

--> assuming module returns a table; this is an exploiters script
for i,v in pairs(require(modulepath)) do
    print(i,v)
end
4 Likes

just a thing to note, you can use += 5, it makes the code shorter.

4 Likes