Last Updated: November 21, 2025
Lua
Lightweight scripting language
Basic Syntax
-- Variables
local name = "John"
local age = 30
local pi = 3.14
-- Functions
function greet(name)
return "Hello, " .. name
end
-- Tables (arrays and objects)
local fruits = {"apple", "banana", "cherry"}
local person = {name = "John", age = 30}
-- Loops
for i = 1, 10 do
print(i)
end
for index, value in ipairs(fruits) do
print(index, value)
end
-- Conditionals
if age >= 18 then
print("Adult")
else
print("Minor")
end
Tables
| Item | Description |
|---|---|
local t = {}
|
Empty table |
t[1] = 'value'
|
Index assignment |
t.key = 'value'
|
Key assignment |
#t
|
Get table length |
table.insert(t, val)
|
Insert value |
table.remove(t, i)
|
Remove at index |
Metatables
local mt = {
__add = function(a, b) return a.value + b.value end,
__tostring = function(t) return "Value: " .. t.value end
}
local obj = {value = 10}
setmetatable(obj, mt)
print(obj + {value = 5}) -- 15
Best Practices
- Use local variables for scope control
- Tables start at index 1, not 0
- Use metatables for OOP patterns
- Lua is commonly embedded in games
💡 Pro Tips
Quick Reference
Lua is the fastest scripting language