Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to access variables in another scripts? In the easiest way possible?

How to access variables in another script? Example:

Script 1:
local x = 1
Script 2:
print(x)

How? It should be THE EASIEST WAY POSSIBLE! I don't need any complicated values to be transfered, only simplest integers!

3 comments

0

See ModuleScripts. They help in storing values that other scripts can direct to.

0

You could also use Bindables, or both bindables and, as xPolarium suggested, modules. Don't use _G

0

He just wants the fastest way of doing it hahahaha loosers weepers finders keepers

1 answer

✓ Accepted answer

Lol

Script 1 : _G.x = 1

Script 2: print(_G.x)

Ok so the _G. Thing before any variable basically access the global namespace table and stores your variable in there which is why you can use it outside of your script :)

Modules are good but they're not as quick to implement as what I said but if you don't mind learning what they are and do then here it is:

You basically make a table and put all your stuff in it your variables and functions etc. Then use require keyword to get your module table and boom you have your own namespace this is quicker to access because the global namespace has a lot more things than just your variable and continuous use without attention to garbage collection can get it cluttered. There you go.

8 comments

0

ModuleScripts can perform better than using _G for what he's looking for. Try including an explanation to what you're doing too.

0

Don't use _G

0

Kk don't use it :/ I get it...

0

require isn't a keyword either.

1

Omg stop picking on me please! I'm a frickin lua learner for God sake 

0

face palm

0

face desk

0

Actually, when I use _G it does exactly what its supposed to do, it works, so I marked this as best answer. I don't want to use ModuleScripts as they are too messy with my current script layout. Thanks for help.

This discussion is preserved as it was. New answers, comments, and votes are closed.