Syntax
local name = "Ada"
local active = true
print(name)Breaking down the syntax
local- Declares a lexically scoped variable.
nil- Represents absence of a value.
..- Concatenates strings (and numbers via conversion).
How it works
Lua names refer to values. local keeps a variable within its lexical scope, and an uninitialised local starts as nil.
Declare
Introduce a local name.
Assign
Give it a value.
Use
Read it within its scope.
Examples
A small working example
Follow the values through this example, then change one input.
local name = "Ada"
local lessons = 3
print(name .. " completed " .. lessons .. " lessons")- local scopes each variable.
- .. concatenates the message.
- print writes the result.
Declaring local values
A small, runnable example of this syntax.
local name = "Ada"
local visits = 3
print(name .. " has " .. visits .. " visits")Best practices
- Declare variables local by default to avoid accidentally changing global state.
- Keep examples small while learning, then combine the idea with a real project.
Runtime & versions
Lua interpreter or a host application. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
Lua reference manualsWhen should I use this?
Prefer local names for script state that should not be placed in the environment.
Common mistakes
A common trap
Only false and nil are false in a condition; zero is truthy.
Incorrect
if 0 then print("zero is false") endCorrected
if 0 then print("zero is truthy in Lua") endCompare approaches
- local name: Limit a variable to a lexical scope.
- Bare name assignment: Assign through the environment when no local binding applies.
Explore deeper
Lua and host languages
A host application may expose extra APIs or a related dialect, such as Luau. Check its documentation before assuming every host has the same syntax and library functions as standard Lua.