Menu
Coddy logo textTech

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 = true

The 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 error
challenge icon

Challenge

Easy

Create 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 = true

Local 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 error

Try 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 block
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals