A Practical Introduction to Lua (Beginner Friendly)

A concise but complete introduction to Lua, with clean explanations and fully working examples.

Lua is a lightweight, embeddable scripting language widely used in game engines (Defold, Roblox, Corona), configuration systems, automation tools, and embedded platforms.

This article gives you a clean, beginner-friendly introduction with complete and runnable examples.

1. Setting Up and Running Lua

Install Lua

  • macOS
brew install lua
  • Linux (Ubuntu/Debian)
sudo apt-get install lua5.4
  • Windows
    Download binaries or use Lua for Windows.

Run a Script

Write hello.lua:

print("Hello, Lua!")

Run:

lua hello.lua

Enter interactive REPL:

lua
> print("Hello REPL")

2. Basic Syntax and Data Types

Comments

-- single-line comment

--[[
multi-line
comment
]]

Variables

x = 10        -- global
local y = 20  -- local

print(x, y)

Core Types

  • nil
  • boolean
  • number
  • string
  • table (Lua’s all-purpose structure)
  • function

Numbers & Operators

local a, b = 10, 3

print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a % b)
print(a ^ b)   -- exponent

Strings

local s1 = "Hello"
local s2 = 'Lua'

local s3 = [[
Multi-line
string
]]

print(s1 .. ", " .. s2 .. "!")  -- concatenation
print(#s1)                      -- length

3. Tables: Arrays, Dictionaries, Objects

Tables are the heart of Lua.

Arrays

local arr = {10, 20, 30}

print(arr[1])
print(arr[2])
print(arr[3])

Dictionaries

local person = {
  name = "Alice",
  age  = 25
}

print(person.name)
print(person["age"])

Mixed Usage

local t = {
  100, 200,
  name = "Bob",
  score = 95
}

print(t[1], t.name, t.score)

4. Control Flow

if / elseif / else

local score = 88

if score >= 90 then
  print("A")
elseif score >= 80 then
  print("B")
else
  print("C or below")
end

while

local i = 1
while i <= 3 do
  print(i)
  i = i + 1
end

repeat … until

local n = 1
repeat
  print(n)
  n = n + 1
until n > 3

for numeric loop

for i = 1, 5 do
  print(i)
end

for generic loop

Iterate array

local arr = {"Lua", "Python", "Go"}

for i, v in ipairs(arr) do
  print(i, v)
end

Iterate dictionary

local config = {host="127.0.0.1", port=8080}

for k, v in pairs(config) do
  print(k, v)
end

5. Functions

Basic Function

local function add(a, b)
  return a + b
end

print(add(3, 5))

Multiple Return Values

local function divide(a, b)
  if b == 0 then
    return nil, "division by zero"
  end
  return a / b, nil
end

local v, err = divide(10, 0)
print(v, err)

Anonymous Function

local result = (function(x, y)
  return x * y
end)(3, 4)

print(result)

6. Modules

Create math_utils.lua:

local M = {}

function M.add(a, b)
  return a + b
end

function M.sub(a, b)
  return a - b
end

return M

Use it:

local m = require("math_utils")

print(m.add(1, 2))
print(m.sub(5, 3))

7. Simple Object-Oriented Style

Lua uses tables + metatables to simulate objects.

local Person = {}
Person.__index = Person

function Person:new(name, age)
  local obj = {name=name, age=age}
  setmetatable(obj, self)
  return obj
end

function Person:say()
  print("Hi, I'm " .. self.name)
end

local p = Person:new("Alice", 25)
p:say()

8. Useful Standard Libraries

String

local s = "hello lua"
print(string.upper(s))
print(string.sub(s, 1, 5))

Math

math.randomseed(os.time())
print(math.random(1, 10))

Table

local t = {}
table.insert(t, "Lua")
table.insert(t, "is fun")
print(table.concat(t, " "))

9. Mini Example: Simple CLI Todo Tool

Save as todo.lua:

local todos = {}

local function add(text)
  table.insert(todos, text)
  print("Added:", text)
end

local function list()
  if #todos == 0 then
    print("No todos.")
    return
  end
  for i, v in ipairs(todos) do
    print(i .. ". " .. v)
  end
end

print("Lua Todo CLI")
while true do
  io.write("> ")
  local line = io.read("*l")
  if line == "quit" then break end
  if line == "list" then
    list()
  elseif line:sub(1,3) == "add" then
    add(line:sub(5))
  else
    print("Commands: add <text>, list, quit")
  end
end

Run:

lua todo.lua

10. Next Steps

After mastering this introduction, continue with:

  • Metatables & metamethods (__index, __add, etc.)
  • Coroutines
  • LuaRocks package manager
  • Using Lua in engines (Defold, LÖVE)

Keep Reading

Follow the engineering thread

Get the next practical Birdor note, or browse the archive for related systems, tooling, and architecture work.

Join newsletter Browse articles