Is there a way to define a variable in one script and be able to use that same variable in a different script?
COMMUNITY QUESTION
Using a variable in multiple scripts?
2 answers
I think you can achieve this by using a module script.
Using module scripts, you can do something like this.
Module script:
local number = 3
Local script/Server Script:
local module = require(PATH TO MODULE SCRIPT)
local number = module.number
print(number) -- will print out 3
And there ya have it!
~Dan_PanMan, advanced scripter.
1 comment
0
The module script has to return a table containing number, like return {number = 3}.
This discussion is preserved as it was. New answers, comments, and votes are closed.

3 comments
Like @Nifemiplayz said, the best way to do this is using ModuleScripts in ReplicatedStorage as it can be accessed by the Client and Server. Another way, less effective for variables, is using RemoteEvents/RemoteFunctions. You could also use _G.VariableName = Value in one script and use a repeat wait() until _G.VariableName in another.
Keep in mind, if you're using _G, they can only be accessed from the same type of script. So, LocalScripts can only access _G from other LocalScripts. ServerScripts can only access _G from other ServerScripts.
Note that ModuleScripts do not replicate their contents, so if you want the variable to be automatically updated and usable in LocalScripts, an IntValue or similar is the easiest solution. If you want clients to be able to influence/cause it to be updated, you need to use a RemoteEvent or RemoteFunction.