Module Scripts, referencing & module variable changes

Hey guys,

Quick question regarding module scripts,
I haven’t had the chance to test this, but was just wondering
if I did something like this.



local modScript = {}
local data = {}

modScript.setTest1 = function(data1)
    data.test1 = data1

modScripot.getTest1 = function()
    return data.test1

return modScript


wait(2) -- wait 2 seconds to allow Script1 to execute first

local modData = require(script.Parent.ModScript)


local modData = require(script.Parent.ModScript)

print(modData.getTest1()) -- output nil

wait(3) -- wait 3 seconds, Script0 should have assigned the value 25 to data.test1

print(modData.getTest1()) -- should output 25

Script 1 calls getTest1(), return nil, not set
Script 0 call setTest1(), set value @ 25
Script 1 calls getTest1(), now returns 25

Is this correct in regards to how module scripts work?
in script1, will modData reference ModScript? in that any future changes made via other scripts will not require calling modData = require(script.Parent.ModScript) again to get the latest changes, as in the above example.

1 Like

I think it all comes in how fast the scripts get executed, so I think this theory could be wrong.

This is why I have a wait in each of the scripts to control execution for this example.

Yes, so long as both scripts are running on the same system - either the server or a specific player.
But doing this exact thing is asking for trouble, definitely don’t rely on one script having executed at a certain time.

Great, Thanks.

I would like to do something similar to the example I’ve given, however I will have a setter flag which will indicate if the module script has been initiated or not.

I figured as much. You should be fine then.