Module 6 · Functions and scope
Local scope
Understand where a variable exists and avoid accidental globals.
What you will learn
- Identify a local variable's scope
- Explain same-name locals in separate scopes
- Avoid accidental global assignments
- scope
- The region of code where a name can be used.
- global
- A name placed in shared environment state rather than a deliberate local scope.
Loading saved progress…
Understand
Read the idea and follow a worked example.
Why this matters
A temporary damage value should not silently overwrite a Coin Quest setting elsewhere in the script.
The mental model
A local variable exists from its declaration through its enclosing block. Function parameters are local to each call.
The same spelling in a nested scope can refer to a different variable, though distinct names are often clearer.
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.
Matching quotes such as "hello" surround a string: a piece of text. The quotation marks tell Luau where the text starts and ends.
print(...) calls the built-in print function. Put the values to show inside its parentheses; separate multiple values with commas. Read the result in Studio's Output panel.
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.
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 label = "Round"local function showPlayerLabel(playerName: string) local label = `Player: {playerName}` print(label)endshowPlayerLabel("Ari")print(label)Line by line
- 3
The outer local belongs to the script scope.
- 5
playerName exists inside each call.
- 6
This inner label is a separate local that shadows the outer name.
- 7
The function prints its inner value.
- 11
Outside the call, label still refers to the outer value.
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 outer local belongs to the script scope.
Line 5: playerName exists inside each call.
Line 6: This inner label is a separate local that shadows the outer name.
Line 7: The function prints its inner value.
Line 11: Outside the call, label still refers to the outer value.
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: Each name has one unambiguous role and no global assignment is created.
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 the final print display?
Try it
Mark each step after you have tried it.
Common mistake
coinTotal = 10Without a prior local declaration, this attempts a global assignment and strict analysis flags it. Declare local coinTotal in the intended scope.
Checkpoint
Submit once you have considered every question. Retries are unlimited.
Recap
- Scope limits where a name exists.
- Parameters and inner locals belong to their function.
- Deliberate locals prevent shared global state.
Next: Next, you will preview callbacks and method-call colon syntax.
Studio Companion
Not connected
Connect once. Compatible challenges will prepare automatically as you reach them.