This is not a limitation -- it's actually a very nice feature.
It means that if two different scripts require the same module, they get the same object.
That means if you had the following modulescript:
local n = 0
function count()
n = n + 1
return n
end
return count
If two different scripts require that module:
A = require(workspace.Counter)
print( A() )
print( A() )
print( A() )
B = require(workspace.Counter)
print( B() )
print( B() )
print( B() )
The counter is not separate for each script -- the second one will continue counting 4 5 6, not have its own "fresh" copy that restarts at 1.
If you don't want to share, it's really easy to change how that works. Just make sure you, instead of returning the thing you don't want shared, return a function which makes a new thing. Then the scripts would look like
function MakeCounter()
local count = 0
return function()
count = count + 1
return count
end
end
return MakeCounter
MakeCounter = require(workspace.MakeCounter)
local A = MakeCounter()
print(A())
print(A())
print(A())
MakeCounter = require(workspace.MakeCounter)
local B = MakeCounter()
print(B())
print(B())
print(B())
Remark: If your modulescripts are pure -- without side-effects -- they don't change anything about themselves (calling functions in any order doesn't affect what answers you get) then whether or not they return something fresh for each script doesn't change how it works at all.
(In general pure functions are nicer to think about, because of things like this, but not always practical)
2 comments
Thanks man. I was actually going to experiment with this when I got home today, but you saved me the time haha.
Oh nice. Thanks for explaining. I can carry on doing what I'm doing safe in the knowledge I'm not breaking anything :D