Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What does this statement do?

Asked by 10 years ago

What does this statement even do:

_G

I mean I know it means global, but what does it mean/do to a game when ran?

2 answers

Log in to vote
3
Answered by
gskw 1046 Moderation Voter
10 years ago

It doesn't do anything on its own, however, when you use it like this:

_G.GlobalVariable = 1

You can use _G.GlobalVariable in every Script / LocalScript depending on which is it used in. Example: Script 1: workspace.GoodScript

_G.GlobalVariable = 1

Script 2: workspace.OtherScript

print(_G.GlobalVariable) -- Prints "1"

Other example: Script 1: workspace.BadScript

GlobalVariable = 1

Script 2: workspace.OtherBadScript

print(GlobalVariable) -- prints "nil"
Ad
Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

_G is a variable (not a statement).

It's equatable to other variables like math or table, in that all it is is a predefined table.

The initial value of _G is the same across all threads & scripts, so modifying a value in the table _G in one script will also modify that key in all others (Limitations: LocalScript on any single client share one separate _G, the server has one separate _G)


Simple example of use:

In one script, NameSetter

_G.zombieName = "Dave";

In all of the zombies in a place:

while not _G.zombieName do
    wait(1);
end
script.Parent.Name = _G.zombieName;

Even though the second script never defines/changes _G.zombieName, the set from NameSetter will affect all of the zombies, and give them the value for their name.

Answer this question