Module 6 · Functions and scope
Mini-project: A damage calculator
Validate numeric inputs and return final health from a typed function.
What you will learn
- Write a typed calculation function
- Guard invalid damage
- Return final health without dropping below zero
- pure function
- A function whose result depends on inputs and does not change outside state.
- clamp
- Limit a number to a defined range.
Loading saved progress…
Understand
Read the idea and follow a worked example.
Why this matters
A focused health calculation is a real game rule that can be tested with disclosed inputs without granting plugin code special privileges.
The mental model
The function receives current health and incoming damage, rejects negative damage, calculates the remainder, and clamps it at zero.
It returns data rather than changing a Humanoid, which keeps the public test safe and predictable.
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.
if is read as “if”: it starts a decision by evaluating a condition.
then is read as “then”: it marks the body that can run when the condition is truthy.
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 tiny example
--!strictlocal DamageCalculator = {}function DamageCalculator.calculateHealth(currentHealth: number, damage: number): number if damage < 0 then return currentHealth end return math.max(0, currentHealth - damage)endreturn DamageCalculatorLine by line
- 3
The module API table starts empty; tables are taught next.
- 5
The public function names two numeric inputs and a numeric result.
- 6
A guard identifies invalid negative damage.
- 7
Invalid input leaves current health unchanged.
- 10
math.max prevents a result below zero.
- 13
The ModuleScript returns its API.
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 table starts empty; tables are taught next.
Line 5: The public function names two numeric inputs and a numeric result.
Line 6: A guard identifies invalid negative damage.
Line 7: Invalid input leaves current health unchanged.
Line 10: math.max prevents a result below zero.
Line 13: The ModuleScript returns its API.
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: Every public case returns a bounded number and the module changes no outside state.
Reading or printing a value leaves it unchanged. Look for assignments, calls that change state, and property updates to explain each change you observe.
Predict before running
What does calculateHealth(20, 30) return?
Try it
Mark each step after you have tried it.
Common mistake
Changing a live Humanoid inside this calculator would mix game state with arithmetic and make testing harder. Return the value; let a server system decide whether to apply it.
Checkpoint
Submit once you have considered every question. Retries are unlimited.
Recap
- Typed functions state their input and output contract.
- Guard clauses handle invalid cases early.
- Pure calculations are easy to test with public cases.
Next: Module 7 groups related values and functions in typed tables.
Studio challenge
A damage calculator
Checks are formative feedback, not cheat-proof certification. You can finish this lesson without the plugin.
- Target
- ReplicatedStorage/LearningLuau/DamageCalculator
- Check mode
- source and output
Challenge state: idle
Studio Companion
Not connected
Connect once. Compatible challenges will prepare automatically as you reach them.