What is _G and _VERSION?

Hi,

So I noticed the Variables (Threads? Functions?):

_G
_VERSION

What are these?
And When and Why should I use this?

3 Likes

_G is to make a variable global. For example:

_G.Var1 = "hello"

Not Sure but isnt this a Global Variable?

Variable1 = "String" -- Global Variable?
local Variable2 "String" -- Local Variable?

Or is it like usable on different scripts?

With _G, you can access a variable in any script.

Also, _Version just gives the Lua version number.

Script 1:

_G.Var1 = "string"

Script 2:

print(_G.Var1) -- OUPUT: string
5 Likes

There are differences between a global variable in a script and a global variable that is in all scripts without defining it, _G is a global variable by Roblox and it is a kind of global variable that appears in all scripts. So _G is still a global variable just not a global variable at the script defined level. Global variables that need to be defined are global variables that will only work in one script. (Unless you define the same variable with the same value in a another script)

I tried this, prints nil

Thanks anyway tho, Will very much be useful

You can define a _G variable on the server and on a different server script you will be able to access it but if you try to access a server variable on a LocalScript or a script with RunContext to Client, then it will print nil. If you define a variable with _G to the server then it will only be accessible from the server, same with the client.

Also, what might have happened was that both the scripts ran at the same time. So, _G.Var1 was not yet defined. Try adding a wait in Script 2 to let Script 1 have time to set the variable.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.