Handling ModuleScript with a Script

So i have modulescript where i have stored data like this:

local Module = {
   Countdown = nil,
   LeftTextLabel = nil
}

function Module.SomethingFunction()
    --// Function logic here
end

return Module

this is an example not actual code and i have 2 scripts where i change values from table before i call functions like this:

local Module = require(--// require here)

Module.Countdown = --// location before calling function
Module.LeftTextLable = --// location before calling function

Module.SomethingFunction()

I have 2 of those Server-Side scripts and both are the same except different locations and i call more functions on second script but also includes function shown in this example script. so my problem is that it feels like it changes properties globally, like for every script and it works differently almost everytime cuz it depends on which script changes values first.

I’m new to ModuleScripts and i feel like it’s very minor issue that i can’t understand here. Thanks to anyone that can give me some tips.

when you change the value and it changes in all scripts that happens because module scripts are required by require() and require share all variables/functions/instances for all scripts
for example when u did (Countdown = nil) this variable is shared between all the scripts that required the module so when you change the value in one script it changes globally

this may be wrong idk

i think that this can be fixed with metatables idk really i havnot used metatable before

when you require a ModuleScript, it runs once and then caches the returned table. This means that all scripts requiring the same module will share the same instance of the returned table. Therefore, changes made to the properties in one script will affect the properties in other scripts that require the same Module aswell.

To prevent this you can basically define another variable in the script where you want to edit the module value

local countdown = Module.Countdown -- lets say Module.Countdown = 5
countdown +=5 

print(Module.Countdown, countdown)

Output:

5, 10 --Module.Countdown, countdown

you can define them in the script as a separate variable and edit them, that works and should fix your issue.

1 Like

I have been stuck on this for 3 days and i thought it was cuz of logic i was using for functions and i have modified code about 100 times. so how can i change it locally then? i want different properties for every script. i think both of us have seen one of those modules where you just adjust few properties and whole modulescript functioning changes. EDIT: i just saw your edit im gonna try that, thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.