Scope and Blocks
Part of the Fundamentals section of Coddy's Lua journey — lesson 86 of 90.
In Lua, you can create new scopes anywhere in your code using do...end blocks. These blocks allow you to control exactly where your local variables can be accessed, giving you precise control over variable scope.
Here's how you can create a new scope with a do...end block:
local outerVar = "I'm outside"
do
local innerVar = "I'm inside"
print(outerVar) -- This works
print(innerVar) -- This works too
end
print(outerVar) -- This works
print(innerVar) -- This would cause an error!The powerful aspect of scope is that you can have local variables with the same name in different blocks, and they remain completely separate:
local name = "Alice"
do
local name = "Bob" -- Different variable!
print(name) -- Prints "Bob"
end
print(name) -- Prints "Alice"Each do...end block creates its own isolated environment for local variables. Variables declared inside a block cannot escape to the outer scope, while variables from outer scopes can be accessed from within inner blocks.
Challenge
EasyCreate nested do...end blocks to demonstrate how local variables with the same name remain completely separate in different scopes.
First, create a local variable named playerLevel with the value 10 in the outer scope and print it.
Then create a do...end block and inside it:
- Declare a new local variable also named
playerLevelwith the value25 - Print this inner
playerLevel
After the inner block ends, print the outer playerLevel again to show that it remained unchanged.
This demonstrates that local variables with identical names in different scopes are completely independent - the inner variable doesn't affect the outer one.
Cheat sheet
Create new scopes anywhere in your code using do...end blocks to control variable scope:
local outerVar = "I'm outside"
do
local innerVar = "I'm inside"
print(outerVar) -- This works
print(innerVar) -- This works too
end
print(outerVar) -- This works
print(innerVar) -- This would cause an error!Local variables with the same name in different blocks remain completely separate:
local name = "Alice"
do
local name = "Bob" -- Different variable!
print(name) -- Prints "Bob"
end
print(name) -- Prints "Alice"Variables declared inside a block cannot escape to the outer scope, while variables from outer scopes can be accessed from within inner blocks.
Try it yourself
-- TODO: Write your code hereThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators 2 Relational & Logic
Equality OperatorsRelational OperatorsThe 'and' OperatorThe 'or' OperatorThe 'not' OperatorShort-Circuit EvaluationTruthy and Falsy ValuesRecap - Simple Logic7Basic Conditional Logic
The if-then StatementThe if-then-else StatementThe elseif StatementRecap - Treasure Chest2Variables and Data Types
What is a Variable?NumbersStringsBooleansThe Value 'nil'The type() FunctionNaming ConventionsRecap - Character Profile5Basic Output
Printing LiteralsPrinting VariablesPrinting Multiple ValuesCombining Strings & VariablesThe tostring() FunctionInputCastRecap - Status ReportRecap - Till 1208String Manipulation Basics
string.len()string.upper & string.lowerstring.sub()string.rep()string.find()Recap - Format Username11Tables 2: Dictionary-Style
Key-Value PairsCreating Dictionary-Style TablAccessing - Bracket NotationAccessing with Dot NotationAdding and Modifying PairsRemoving Pairs with nilRecap - Item Properties14Variable Scope
Global VariablesLocal VariablesWhy Use Local Variables?Scope and BlocksRecap - Scope Puzzle3Operators 1 Arithmetic & Conc
Arithmetic OperatorsModulo OperatorExponentiation OperatorString ConcatenationOperator PrecedenceRecap - Simple Calculations6Project: Character Stats Disp
Welcome MessageDeclare Character Stats9Functions Basics
Declaring a FunctionCalling a FunctionFunctions with ParametersFunctions with Multiple ParamsThe 'return' StatementRecap - Area Calculator