Module 12 · Modules, persistence, and the capstone
ModuleScripts and require()
Return a focused API, understand caching, and avoid work merely from requiring a module.
What you will learn
- Return an API from a ModuleScript
- Explain require caching
- Avoid side effects during require
- require
- Load a ModuleScript and receive its returned value.
- cache
- A stored result reused for later requires in the same Luau environment.
Loading saved progress…
Understand
Read the idea and follow a worked example.
Why this matters
Coin Quest calculations and configuration should be reusable without copying code or unexpectedly starting systems.
The mental model
A ModuleScript runs the first time it is required in one environment and returns one value. Later requires there receive the cached return value.
Define functions and data at module load, but put world mutations and event connections behind explicit setup functions.
New words and symbols
--!strict asks Studio to check your code for type mistakes. It is an instruction for the editor and produces no Output message.
local is read as “local”: it creates a name that is available only in the current code scope.
end is read as “end”: it closes a block such as a decision, loop, or function.
return is read as “return”: it ends the current function call and sends a value to its caller.
{ } is read as “opening and closing braces”: it creates a table value around its entries.
A colon in a declaration, such as coins: number, introduces a type annotation: the kind of value that name is allowed to hold.
= is read as “is assigned”: it evaluates the right side and stores that value in the name on the left.
A tiny example
--!strictlocal ScoreMath = {}function ScoreMath.fromCoins(coins: number): number return math.max(0, coins) * 10endreturn table.freeze(ScoreMath)Line by line
- 3
The module API begins as a local table.
- 5
A focused public function accepts and returns numbers.
- 6
The function calculates without changing world state.
- 9
The immutable API is returned to callers.
Read the code aloud
Read one statement at a time: name the value being read, what the operation does, and where its result goes. Keep identifiers exactly as written so you can find them again in the code.
Pause at calls, conditions, and loops. Explain when the body runs before moving to the next statement; use the line notes above when a symbol is unfamiliar.
Execution trace
Start with the statements outside function bodies. A function definition creates a callable value; its body runs when a call, event, or scheduled task reaches it.
Trace the arguments at each call and the branch or loop inside that call. Return to the caller when the function returns. For an event, use the action named in Try it to trigger the callback.
Line 3: The module API begins as a local table.
Line 5: A focused public function accepts and returns numbers.
Line 6: The function calculates without changing world state.
Line 9: The immutable API is returned to callers.
State before and after
Before running, identify the starting values and any existing Studio objects this example needs. List what you expect to change.
After your practice change, check this result: Both callers use the same focused API and requiring it performs no world mutation.
Reading or printing a value leaves it unchanged. Look for assignments, calls that change state, and property updates to explain each change you observe.
Requiring the module
--!strictlocal ReplicatedStorage = game:GetService("ReplicatedStorage")local ScoreMath = require(ReplicatedStorage.ScoreMath)print(ScoreMath.fromCoins(4))Predict before running
What does a second require receive in the same environment?
Try it
Mark each step after you have tried it.
Common mistake
Connecting events or creating Instances at module top level hides lifecycle work inside require. Expose an explicit setup method instead.
Checkpoint
Submit once you have considered every question. Retries are unlimited.
Recap
- ModuleScripts return APIs or data.
- require caches the returned value per environment.
- Setup side effects belong behind explicit methods.
Next: Next, you will give module services explicit Init and Start phases.
Studio Companion
Not connected
Connect once. Compatible challenges will prepare automatically as you reach them.