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
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
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance