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

Reading/Writing Variables between ModuleScripts?

Basically, If I have a variable defined in a ModuleScript, can any ModuleScripts under that read/write variables from it's parent?

ModuleScript1>ModuleScript2

Like, for example, If I have ModuleScript1 define this:

Variable = true

Can ModuleScript2 do this?

Variable = false

My script is more complicated than this, so this honestly might work. Idk, because I cant really test this. I'm sure it's possible, but I cant figure out how. Ik global functions are a thing, but are global variables a thing? I've tried doing 'require(script.Parent)' , but that doesn't seem to work. I've tried a few things, but so far nothin.

If you know a way to do this, I would really appreciate it if you'd let me know. Thanks! :)

NOTE: I have indeed read the wiki article on ModuleScripts. So, yea. Also, if anything here needs clarifying, lemme know. :)

2 answers

✓ Accepted answer

I highly suggest against using Global variables. They're useful, but bad practice.

Instead, make the variable a member of the Table you're returning.

module = {}

module.Variable = true

return module

Then the Script that requires this module can set the Variable using <moduleVar>.Variable.

3 comments

0

Now that's clever! Thanks! Very helpful too. :D

0

Question: Does this work when trying to use it from a child module?

0

What do you mean a "child module"? Modules are self-contained. They're basically a partially global Table, is all.

GLobal Variables exist!

_G.Hello = "Hello, it's me"

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