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

How to share variables through require?

Asked by 7 years ago

So I'm making private modules, but I am not sure how to share variables.

I have tried _G but that does not seem to work

I have also tried doing

1require((29185912) (Product_ID)

Also once I do it, how would I transfer it the module script and use it?

1 answer

Log in to vote
0
Answered by 7 years ago

We use what is called accessors and mutators ie get data (read only) set data (validate then set copy) this is the basics of OOP.

An example would be :-

01local vars = {}
02local f = {}
03local get = {}
04local set = {}
05 
06-- add variables
07vars.a = true
08 
09-- return a variable
10function get.getA()
11    return vars.a
12end
13 
14-- set variable a
15function set.setA(val)
View all 25 lines...

Script:-

01-- for quicker access make variables to point to our tables
02local module = require(script.Parent.ModuleScript) -- unpack our table into its components
03local get, set, f = module.get, module.set, module.f
04 
05print(get.getA())
06f.printA()
07 
08set.setA('Hi')
09f.printA()
10set.setA(false)
11f.printA()
12print(get.getA())

Working with private modules can be difficult as allowing the user to input data can leave it vunrable so you should test for this.

I hope this helps

Ad

Answer this question