New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
1

How to use the "_G" in the script editor ?

Asked by 1 year ago

I have a variable in my script editor called "_G" but i don't know how to use it so i tried this :

print(_G)

but the output says :

table: 1C06E8EC

It is a table ? I need to understand how "_G" works

0
No don't use _G User#19524 150 — 1y

2 answers

Log in to vote
1
Answered by 1 year ago
Edited 1 year ago

_G is a shared table. Accessible through all scripts in the game

_G.NAME_OF_VARIABLE = "VALUE"

e.g

_G.StringThatSaysHi = "Hi"

For a better visualization, think of it as a regular table

local myFruits = {}
    myFruits.Apples = 2
    myFruits.Bananas = 3
0
No, please no. Don't use _G, use ModuleScripts instead User#19524 150 — 1y
0
I never recommended it, just explained it YTRaulByte 274 — 1y
0
Thanks xJathur95x 129 — 1y
0
Does hackers can acces to the _G ? xJathur95x 129 — 1y
Ad
Log in to vote
0
Answered by 1 year ago

_G is a global table that is accessible from all scripts of its kind. The server and every client has their own _G table. You can't use it as your own variable because the script will think that you're referencing the table.

If you wanted to make use of _G, you could store a variable or function from one script and quickly pass it to another, like this:

Server Script #1

_G.Money = 40

Server Script #2

print(_G.Money)
-- prints 40
0
Shouldn't use _G though. Use ModuleScripts instead User#19524 150 — 1y
0
LocalScripts don't commnicate `_G`; although they're able to use it, they don't share it with each other like with server scripts. TheeDeathCaster 2309 — 1y
0
It seemed to work for me a while ago, hm Trainsparency 39 — 1y

Answer this question