I was doing a project working on a module and I’ve known for a while that module variables are shared across different instances of require()
The way this behavior works is if you set a local variable within the module to a different value it replicates to all instances running the module. To fix this behavior you can simply do this instead of refactoring your code.
--module code example
local PuddleConsole={}
function PuddleConsole.Initialize()
local Puddle = {Set={},Settings={},Player={}}
return Puddle
end
return PuddleConsole
To require this as normal is illustrated by this example.
local PuddleSys=game.ReplicatedStorage.PuddleSystem
local Puddle=require(PuddleSys).Initialize()
Why would you do this?
You may experience bugs if you require multiple instances of the same module that manipulates variables located outside each function. Doing this ensures they are inside the module function.
The behavior of modules in this regard I’m not sure if it extends to tables. Or if the module required is running in a different local machine (actor).
This is mainly a post about me finding a bug and fixing the bug. This solution may not be for everyone but may assist someone looking for a solution or informative to those who were not aware of this being a factor.