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

Reading/Writing Variables between ModuleScripts?

Asked by 8 years ago

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

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
8 years ago

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.

0
Now that's clever! Thanks! Very helpful too. :D AwsomeSpongebob 350 — 8y
0
Question: Does this work when trying to use it from a child module? AwsomeSpongebob 350 — 8y
0
What do you mean a "child module"? Modules are self-contained. They're basically a partially global Table, is all. adark 5487 — 8y
Ad
Log in to vote
0
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
8 years ago

GLobal Variables exist!

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

Answer this question