How do I use on variable in multiple scripts? (beginner scripter)

Hey everyone! I’m new to scripting so this is probably a dumb question, but I have a variable in one script (in StarterGui) and another script (in workplace)

Theres a variable in startergui that I want to use in workspace. I’m wondering whether the variable belongs in ServerStorage, or if im doing something completely wrong lol. Thanks!

You might need to learn variables again no clue really what your talking about. Variables contain a line of code of your choice and by calling it at anytime IN THE SAME SCRIPT the line of code can be ran or whatever is in your variable.

Yeah, I already mentioned that I have no clue what I’m talking about. So, I cant have a variable in multiple scripts? I’ve seen some posts saying that they cant be stored in ServerScriptService but it doesn’t seem to be working.

so you can store normal scripts in serverscriptservice and its basically just a storage spot for scripts

You can use globals. Though they only replicate to their respective script type.

_G.Map = workspace.Map
--another script
for _,v in _G.Map:GetChildren() do
    print(tostring(v))
end
1 Like

You can use module scripts to share variables between scripts. requiring a modulescript from any script returns a reference to the same table (Note that each RunContext gets its own new reference/table created). It’s not really recommended to use globals, mostly due to race conditions

While this does work, it’s not recommended to avoid race conditions (the order the scripts modify the variable isn’t always the same).

I would either use module scripts, value objects (ex: IntValue), or bindable events

1 Like