Skip to content

Lua Tutorials — Complete Beginner to Advanced Guide

In this tutorial series, you'll learn Lua from the ground up. Lua is a lightweight embeddable scripting language designed for extensibility -- used in games, embedded devices, Redis scripts, and configuration files.

1. What is Lua?
History, design, use cases
2. Setup
Install, REPL, first script
3. Syntax
Variables, types, operators
4. Tables
Arrays, dictionaries, mixed tables
5. Functions
First-class, closures, varargs
6. Metatables
Metamethods, operator overloading
7. OOP with Prototypes
self, colon syntax, inheritance
8. Coroutines
Cooperative multitasking, yield
9. File I/O
Read, write, serialization
10. Modules
require, module pattern, packages
11. C API
Embedding, extending, Lua stack
12. Love2D
Game framework, graphics, input
13. LuaRocks
Package manager, dependencies
14. Strings
Pattern matching, manipulation
15. Numbers
Arithmetic, math lib, precision
16. Booleans & Nil
Truthiness, logical operators
17. Control Flow
if, elseif, else, while, repeat
18. Loops
for numeric, for generic, break
19. pairs vs ipairs
Iterator functions, traversal
20. Varargs
..., select, unpack
21. Closures
Lexical scoping, upvalues
22. Tail Recursion
Optimization, proper tail calls
23. Error Handling
pcall, xpcall, error handling
24. Debugging
Debug lib, stack traces
25. Standard Libraries
math, string, table, os, io
26. Bit Operations
bit32, bitwise operators
27. LuaJIT
JIT compiler, FFI, performance
28. Testing
busted, unit testing, TDD
29. String Patterns
Pattern matching, captures
30. Mini Projects
Config parser, game, CLI tool

Let's begin with Lesson 1.

Published Topics

What is Lua? A Lightweight Scripting Language for Embedded Systems and Games

Lua is a lightweight embeddable scripting language designed for extensibility, used in games, embedded devices, and configuration files with a simple C API.

✓ Live

Lua Setup Guide — Install Lua and Run Your First Script

Learn how to install Lua 5.4, run scripts with the Lua interpreter, explore the REPL, and set up a development environment for Lua programming.

✓ Live

Lua Syntax Guide — Variables, Types, Operators, and Comments

Learn Lua syntax fundamentals including variables, data types (nil, boolean, number, string, table, function), operators, and commenting conventions.

✓ Live

Lua Tables Guide — Arrays, Dictionaries, and Mixed Data Structures

Lua tables are the universal data structure serving as arrays, dictionaries, objects, modules, and sets -- with 1-based indexing and flexible key-value storage.

✓ Live

Lua Functions Guide — First-Class Functions, Closures, and Varargs

Lua functions are first-class values that can be stored in variables, passed as arguments, and returned -- supporting closures, multiple returns, and variable arguments.

✓ Live

Lua Metatables Guide — Operator Overloading and Custom Behavior

Lua metatables control table behavior through metamethods like __add, __index, and __tostring, enabling operator overloading and prototypal inheritance.

✓ Live

Lua OOP Guide — Object-Oriented Programming with Prototypes

Lua implements OOP through prototype-based inheritance using tables, metatables, and colon methods -- supporting encapsulation, polymorphism, and mixins.

✓ Live

Lua Coroutines Guide — Cooperative Multitasking and Yield

Lua coroutines enable cooperative multitasking with explicit yield points -- functions that pause execution and resume later without blocking the main thread.

✓ Live

Lua File I/O Guide — Reading, Writing, and Processing Files

Lua file I/O covers reading and writing files through the io library, with simple (file:read) and complex (file:lines, templates) patterns for text and binary data.

✓ Live

Lua Modules Guide — require, Module Patterns, and Package Organization

Lua modules organize code into reusable files with require(), using table-based module patterns and the package system for managing dependencies and namespaces.

✓ Live

Lua C API Guide — Embedding and Extending Lua in C Programs

The Lua C API lets C programs embed Lua as a scripting engine, expose C functions to Lua, and call Lua code from C using the lua_State and stack-based communication.

✓ Live

Lua Love2D Guide — Game Development with the Love2D Framework

Love2D is a Lua-based game framework for 2D game development with graphics, audio, input handling, and physics, making game prototyping fast and accessible.

✓ Live

Lua LuaRocks Guide — Package Management for Lua Projects

LuaRocks is the package manager for Lua -- installing, managing, and distributing Lua modules through rock files with automatic dependency resolution.

✓ Live

Lua Strings Guide — Pattern Matching and String Manipulation

Lua strings are immutable byte sequences with a powerful pattern-matching library similar to regex, supporting operations like find, match, gsub, and gmatch.

✓ Live

Lua Numbers Guide — Arithmetic, Math Library, and Numeric Precision

Lua numbers use double-precision floating point with 64-bit integers in Lua 5.3+, supported by a math library for trig, rounding, roots, and random numbers.

✓ Live

Lua Booleans and Logical Operators Guide — Truthiness and Conditionals

Lua booleans use true and false values with logical operators (and, or, not) that short-circuit and can return non-boolean operands, enabling concise conditional expressions.

✓ Live

Lua Control Flow Guide — if, elseif, and Conditional Logic

Lua control flow uses if-elseif-else-end blocks for conditional execution, supporting multiple branches with comparison operators and logical combinations for complex decisions.

✓ Live

Lua Loops Guide — while, for, and repeat-until Control Structures

Lua loops include while, numeric for, generic for, and repeat-until, with break and goto for flow control, supporting iteration over tables and numeric ranges.

✓ Live

Lua Advanced Tables Guide — Table Functions and Data Structure Patterns

Lua advanced tables cover table library functions (insert, remove, sort, concat), weak references, and common patterns like sets, queues, and memoization for efficient data handling.

✓ Live

Lua I/O Library Guide — File Reading, Writing, and Stream Operations

Lua I/O library provides file handling through io.open, io.lines, io.read, io.write, and file handles, supporting reading and writing with different modes and formats.

✓ Live

Lua OS Library Guide — Time, Date, and System Operations

Lua OS library provides os.time, os.date, os.clock, os.difftime for time, os.execute for commands, os.rename and os.remove for files, and os.getenv for environment variables.

✓ Live

Lua Debugging Guide — Debug Library and Diagnostic Techniques

Lua debug library provides hook-based debugging with debug.sethook, debug.getinfo, debug.getlocal, and debug.traceback for inspecting runtime state and diagnosing issues interactively.

✓ Live

Lua Error Handling Guide — Protected Calls and Exception Patterns

Lua error handling uses pcall (protected call) and xpcall with error messages and stack traces, providing try-catch-like semantics without exception syntax for robust error recovery.

✓ Live

Lua Advanced OOP Guide — Inheritance, Mixins, and Metaclass Patterns

Lua advanced OOP extends basic prototype-based objects with multiple inheritance via metatables, mixins through table merging, and metaclass patterns for creating class-like hierarchies.

✓ Live

Lua Weak Tables Guide — Weak References, Caches, and Memory Management

Lua weak tables use __mode metatable field with 'k' (key), 'v' (value), or 'kv' to enable garbage collection of unreferenced entries, ideal for caches and object associations.

✓ Live

Lua Garbage Collection Guide — GC Tuning and Memory Management

Lua garbage collection is incremental with configurable pause, step multiplier, and step size via collectgarbage, supporting manual collection and generational mode in Lua 5.4+.

✓ Live

Lua Bit Operations Guide — Bit32 Library and Bit Manipulation

Lua bit32 library provides bitwise operations (band, bor, bxor, bnot, lshift, rshift) on 32-bit integers, essential for flags, networking protocols, and embedded systems programming.

✓ Live

Lua Persistence Guide — Serialization and Data Storage Techniques

Lua persistence covers table serialization to strings, file-based storage with io library, and embedded data through Lua scripts that reconstruct state on load for configuration and caching.

✓ Live

Lua UTF-8 Guide — Unicode Support and String Encoding

Lua UTF-8 library (utf8) in Lua 5.3+ provides functions for Unicode handling including character offsets, codepoint extraction, and iteration over grapheme clusters in encoded strings.

✓ Live

Lua Goto and Labels Guide — Advanced Flow Control and Jump Statements

Lua goto statement with ::label:: syntax enables unconditional jumps within a function, useful for breaking out of nested loops, implementing finite state machines, and error recovery patterns.

✓ Live

All 30 topics in Lua Tutorials — Complete Beginner to Advanced Guide are published.