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

Can I reference a non local variable of a different script?

Is it possible to reference a variable from a different script from my current one? for example if in script 1 I have VARIABLE1 = THING1 in my second script can I somehow tell it to use the VARIABLE1 variable? Thanks guys!

1 comment

0

Im sure you can do that with module scripts

1 answer

✓ Accepted answer

You have a couple options regarding what you are looking for.

First would be what we call Global Variables. You can declare a global variable by placing _G. before the name of the variable. You will be able to call this variable from almost any script within your game file.

Script 1:

_G.bool = true -- Creates a global variable and assigns 'true' as the value

Script2:

print (_G.bool) --Should print 'true'

Another option would be to create a Module Script with variables you wish to use in multiple scripts. These work somewhat similar to global variables when you refer to them in other scripts.

Lets say you made a module script returned the array of variables called _M. After you pull the module script into your new script, you can refer to the variables as _M.variable or whatever you named the variable you created in the module script.

Example of module script:

_M = {
	bool = true
}

return _M

How to call the variable from another script:

_M = require('Path to Module Script') -- This will call the module script and return the variables

print (_M.bool) --This should print 'true'

Let me know if this answers your question.

2 comments

1

(For most cases, global variables are not good practice)

0

so wait if I wanted the variables "child" and "current" to be equal on a global scale would I do _G.child = current ?

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