Lua Cheat Sheet
Lua syntax, tables, metatables, closures, and coroutines for lightweight embedded scripting and game engine development.
1 PageIntermediateApr 5, 2026
Basic Syntax
Variables, control flow, and printing.
lua
local age = 30local name = "Ada"local pi = 3.14159if age >= 18 then print(name .. " is an adult")endfor i = 1, 5 do print("Count: " .. i)end
Tables
Lua's single, versatile data structure.
lua
-- tables are Lua's only data structure (array + map)local nums = {5, 3, 1, 4, 2}table.insert(nums, 6) -- appendtable.remove(nums, 1) -- remove index 1print(#nums) -- length operatorlocal person = {name = "Ada", age = 30}print(person.name) -- dot syntaxperson["age"] = 31 -- bracket syntaxfor i, v in ipairs(nums) do print(i, v) end -- sequentialfor k, v in pairs(person) do print(k, v) end -- all keys
Metatables
Operator overloading and OOP patterns.
lua
local Vector = {}Vector.__index = Vectorfunction Vector.new(x, y) return setmetatable({x = x, y = y}, Vector)endVector.__add = function(a, b) return Vector.new(a.x + b.x, a.y + b.y)endlocal v1 = Vector.new(1, 2)local v2 = Vector.new(3, 4)local v3 = v1 + v2 -- uses __add metamethod
Core Keywords
Common Lua language keywords.
- local- declares a variable scoped to the current block
- function- defines a function; supports closures over enclosing locals
- nil- absence of a value, also used to delete table keys
- #- length operator for tables/strings
- ..- string concatenation operator
- require- loads a module and caches the result
- coroutine.create/resume/yield- cooperative multitasking primitives
Closures & Upvalues
Functions capturing enclosing local variables.
lua
local function counter() local n = 0 return function() n = n + 1 return n endendlocal next = counter()print(next()) -- 1print(next()) -- 2-- Multiple closures share upvalueslocal function pair() local v = 0 return function() v = v + 1 end, function() return v endend
Coroutines
Cooperative multitasking with yield/resume.
lua
local co = coroutine.create(function(a) print("start", a) local b = coroutine.yield(a + 1) print("resumed", b) return b * 2end)print(coroutine.resume(co, 10)) -- true 11print(coroutine.resume(co, 5)) -- true 10print(coroutine.status(co)) -- dead-- Generator pattern with wraplocal gen = coroutine.wrap(function() for i = 1, 3 do coroutine.yield(i) endend)
OOP with Metatables
Class-like inheritance using __index.
lua
local Animal = {}Animal.__index = Animalfunction Animal.new(name) return setmetatable({ name = name }, Animal)endfunction Animal:speak() return self.name .. " makes a sound"endlocal Dog = setmetatable({}, { __index = Animal })Dog.__index = Dogfunction Dog:speak() return self.name .. " barks" endlocal d = setmetatable(Animal.new("Rex"), Dog)print(d:speak()) -- Rex barks
String Library
Core string manipulation functions.
- string.format(f, ...)- printf-style formatting returning a string
- string.find(s, p)- locate a pattern, returns start/end indices
- string.gsub(s, p, r)- global substitution, returns result and count
- string.gmatch(s, p)- iterator over all pattern matches
- string.sub(s, i, j)- extract a substring by 1-based indices
- string.rep(s, n)- repeat a string n times
- #s- length operator for strings and sequences
Pro Tip
Use `local` for all variables you can — globals in Lua are stored in a table lookup (_G) and are noticeably slower to access than locals.
Was this cheat sheet helpful?
Explore Topics
#Lua#LuaCheatSheet#Programming#Intermediate#BasicSyntax#Tables#Metatables#CoreKeywords#Functions#Concurrency#CommandLine#CheatSheet#SkillVeris