Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What does it mean when people say that module scripts cache?

Asked by 5 years ago

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?

0
If I have not adequately answered your question, please let me know. theCJarmy7 1293 — 5y

1 answer

Log in to vote
1
Answered by
theCJarmy7 1293 Moderation Voter
5 years ago
Edited 5 years ago

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.

0
Could you explain that a little better and maybe give some reasons? User#21908 42 — 5y
0
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. User#19524 175 — 5y
0
Thank you theCJarmy7! User#21908 42 — 5y
Ad

Answer this question