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

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

Asked by
NorteX_tv 101
5 years ago

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!

0
See ModuleScripts. They help in storing values that other scripts can direct to. xPolarium 1388 — 5y
0
You could also use Bindables, or both bindables and, as xPolarium suggested, modules. Don't use _G User#19524 175 — 5y
0
He just wants the fastest way of doing it hahahaha loosers weepers finders keepers xxcoordinatorxx 135 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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.

0
ModuleScripts can perform better than using _G for what he's looking for. Try including an explanation to what you're doing too. xPolarium 1388 — 5y
0
Don't use _G User#19524 175 — 5y
0
Kk don't use it :/ I get it... xxcoordinatorxx 135 — 5y
0
require isn't a keyword either. User#19524 175 — 5y
View all comments (4 more)
1
Omg stop picking on me please! I'm a frickin lua learner for God sake  xxcoordinatorxx 135 — 5y
0
*face palm* User#23365 30 — 5y
0
*face desk* SummerEquinox 643 — 5y
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. NorteX_tv 101 — 5y
Ad

Answer this question