The title says most of it but I have little note to add. What does it mean when people say module scripts return the same value when called in the same environment multiple times and how does that affect me?
What does it mean when people say that module scripts cache?
1 answer
It means a module script will always return the same value.
--In a module script
return math.random(1,9)
--In a script
while true do
print(require(script.ModuleScript))
wait(1)
end
And it only prints 1 number, every second, the same number.
--Another example moduleScript
return game.Workspace.Value.Value
--Script
while true do
print(require(script.ModuleScript))
--prints the original value repeatedly
game.Workspace.Value.Value = game.Workspace.Value.Value .. " and antoher one"
wait(1)
end
So here, the module script doesn't run twice, it only looks up what it returned previously, and gives returns that, even though the value it looks at changes.
Explain it a bit better: A module script runs once, and only once. Adding a print statement to the beginning will show this:
print("Important code bits")
--The above only prints once
return game.Workspace.Value.Value
And if it is required again, it just returns what it did the previous time it was required.
Reasons? The same reasons anything caches, to improve performance. Module scripts shouldn't be used to return values like in the above examples anyway.
As for the same environment thing, a module script would be ran once from the server, and once on each client that requires it.
return game.Players.LocalPlayer
Would return nil if required from a serverScript, and return the local player if required from a LocalScript.
3 comments
Could you explain that a little better and maybe give some reasons?
A ModuleScript takes on the behaviour of a script/localscript depending on what environment it is used. For example if you used Local player it’s taking behaviour of a local script.
Thank you theCJarmy7!
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
If I have not adequately answered your question, please let me know.