Local Variables
Part of the Fundamentals section of Coddy's Lua journey — lesson 84 of 90.
Lua provides the local keyword to create variables with limited scope. When you declare a variable with local, it can only be accessed within the specific block where it was created.
Here's how you create a local variable:
local playerHealth = 100
local isGameActive = trueThe key difference is that local variables are confined to their block. For example, if you create a local variable inside a do...end block, it cannot be accessed outside of that block:
do
local secretCode = 1234
print(secretCode) -- This works
end
print(secretCode) -- This would cause an errorChallenge
EasyCreate a do...end block and declare a local variable named secretMessage inside it with the value "Hidden data". Within the same block, print the value of secretMessage.
After the do...end block ends, attempt to print secretMessage again. Since local variables are confined to their block scope, this second print statement will output nil because the variable is no longer accessible outside its block.
Cheat sheet
Use the local keyword to create variables with limited scope:
local playerHealth = 100
local isGameActive = trueLocal variables are confined to their block and cannot be accessed outside:
do
local secretCode = 1234
print(secretCode) -- This works
end
print(secretCode) -- This would cause an errorTry it yourself
-- TODO: Write your code here
-- Create a do...end block with a local variable secretMessage
-- Print secretMessage inside the block
-- Then try to print secretMessage outside the blockThis 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