TExcelTAD
Home › Lua › Lua Variables

Lua Variables

A variable is a name that refers to a value. Lua variables have no declared type: the value has a type, and the same variable can hold a number now and a string later.

lua · try it yourself
local city = "Lisbon"
local population = 545923
local isCapital = true

print(city, population, isCapital)

population = population + 1000   -- change the value
print(population)
Lisbon	545923	true
546923

local vs global

Lua has two kinds of variables:

  • local variables, created with the keyword local, live only in the block where they are declared.
  • global variables are created by simply assigning to a name that isn't local. They are visible everywhere.
lua · try it yourself
local level = 1      -- local: preferred
lives = 3            -- global: visible to all code

print(level, lives)
1	3

Tip: Always write local unless you have a real reason not to. Locals are faster, and they can't be accidentally overwritten by some other part of the program. The next lesson covers scope in depth.

nil: the absence of a value

A variable that was never assigned holds nil. Reading an unknown name is not an error in Lua; it just gives nil:

lua · try it yourself
local nickname
print(nickname)
print(undefinedVariable)

nickname = "Ace"
print(nickname)
nickname = nil       -- "delete" the value
print(nickname)
nil
nil
Ace
nil

This is convenient but dangerous: a typo such as pritn or scroe silently produces nil instead of an error at that point. Linters such as luacheck and the Lua Language Server catch most of these.

Multiple assignment

Lua can assign several variables in one statement. All expressions on the right are evaluated before any assignment happens, which makes swapping values a one-liner:

lua · try it yourself
local x, y, z = 1, 2, 3
print(x, y, z)

x, y = y, x              -- swap
print(x, y)

local a, b, c = 10, 20   -- too few values: c gets nil
print(a, b, c)

local p, q = 1, 2, 3     -- too many values: 3 is dropped
print(p, q)
1	2	3
2	1
10	20	nil
1	2

Naming conventions

Style Typical use Example
lower_snake_case locals and functions (standard library style) max_speed, string.format
camelCase common in games and Roblox code playerName
PascalCase "classes" and modules Vector, Account
UPPER_CASE constants by convention MAX_PLAYERS
_ a value you ignore for _, v in ipairs(t)

Pick one style and stay consistent inside a project.

Constants with <const>

Lua 5.4 added attributes for locals. <const> makes a variable that can't be reassigned; the compiler rejects any later assignment:

lua · try it yourself
local MAX_PLAYERS <const> = 4
print("Up to " .. MAX_PLAYERS .. " players")

local chunk, err = load("local LIMIT <const> = 10; LIMIT = 20")
print(err)
Up to 4 players
[string "local LIMIT <const> = 10; LIMIT = 20"]:1: attempt to assign to const variable 'LIMIT'

The other attribute, <close>, calls a cleanup method when the variable goes out of scope; it is covered in the error handling lesson.

Note: <const> only protects the variable, not the value. A <const> table can still have its fields changed.

Test yourself

What is the value of a variable that was never assigned?

  1. 0
  2. ""
  3. nil
  4. It is an error to read it

After local a, b = 5, what is b?

  1. 5
  2. 0
  3. nil
  4. A syntax error

How do you swap x and y in Lua?

  1. swap(x, y)
  2. x, y = y, x
  3. x = y; y = x
  4. x <-> y