Lua Tutorial
Learn Lua step by step: 32 lessons in 11 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
Getting Started
- Introduction to Lua Meet Lua, the small, fast scripting language inside games, Redis and Neovim. Learn its design goals, Lua 5.4 versus LuaJIT, and how scripts are run.
- Your First Lua Program Write and run your first Lua program: print and io.write, comments, statements and newlines, running a file with lua, and using the interactive REPL.
- Variables and Types Learn Lua variables and the eight basic types: local versus global, nil, booleans, numbers, strings, tables, functions, type() and multiple assignment.
Values and Operators
- Numbers and the math Library Learn how Lua 5.4 numbers work: integer and float subtypes, floor division //, modulo, math.floor and math.random, tonumber, and integer overflow wrap.
- Strings Learn Lua strings: quote styles, long brackets, escapes, the .. concatenation operator, the # length operator, string.sub, upper, rep, format and immutability.
- Operators and Truthiness Learn Lua operators: comparison, and, or, not with short-circuit rules, why only nil and false are falsy, the a and b or c idiom, and operator precedence.
Control Flow
- Conditionals Make decisions in Lua with if, elseif and else, nest conditions cleanly, and replace the missing switch statement with a table of functions.
- Loops Learn Lua's three loops: while, repeat-until and the numeric for with a step, plus break, the goto continue idiom and how loop variables are scoped.
- Iterating with pairs and ipairs Walk over Lua tables with ipairs and pairs, learn what order to expect, how to modify a table safely during a loop, and how the generic for works.
Functions
- Functions Define and call Lua functions, pass parameters, return several values at once, understand value adjustment, and use the call syntax shortcuts.
- Varargs and Closures Write Lua functions that take any number of arguments with ..., use select and table.pack, and build counters and factories with closures and upvalues.
- Recursion and Tail Calls Write recursive Lua functions, avoid the local function trap, see why proper tail calls never overflow the stack, and speed up recursion with memoization.
Tables
- Tables as Arrays and Dictionaries Learn Lua tables: constructors, 1-based indexing, string keys, the length operator and its holes, and how assigning nil removes a key. Includes runnable stand-ins.
- The table Library Learn the Lua table library: table.insert, remove, concat, sort with a comparator, unpack and move, plus why table.sort is not stable. Runnable JavaScript stand-ins.
- Tables in Practice Use Lua tables as sets, stacks, queues and matrices, and learn the difference between shallow and deep copies and why tables are references.
Metatables and OOP
- Metatables and Metamethods Understand Lua metatables: setmetatable, __index and __newindex for lookups and writes, arithmetic and comparison metamethods, __tostring, __call and __len.
- Objects and Classes Build classes in Lua with tables and metatables: the colon call syntax and self, class tables with __index, constructors, and private state with closures.
- Inheritance and Mixins Learn inheritance in Lua with metatable chains: subclasses, calling a parent method, polymorphism, mixins that copy functions, and composition over inheritance.
Errors, Modules and Coroutines
- Error Handling Learn how Lua raises and catches errors with error, assert, pcall and xpcall, how to throw tables as error objects, read tracebacks and clean up with to-be-closed variables.
- Modules and require Learn how Lua modules work: returning a table from a file, require and package.loaded caching, package.path, avoiding globals and using _ENV for sandboxes.
- Coroutines Learn Lua coroutines: create, resume, yield, status and wrap, passing values both ways, writing generators and producers, and a simple cooperative scheduler.
Standard Library and Tools
- Files and I/O Learn Lua file handling: io.open modes, read formats, lines, write and close, io.lines, plus the os library for time, dates, clock and environment variables.
- String Patterns Learn Lua string patterns: character classes, quantifiers, captures, anchors, %b and %f, and the find, match, gmatch and gsub functions. How they differ from regex.
- Debugging, Performance and Common Mistakes Find bugs faster in Lua: print debugging, debug.traceback, accidental globals, 1-based off-by-one errors, the # operator and holes, table.concat and local caching.
Real-World Lua
Projects
- Project: A To-Do List Manager Build a command-driven to-do list in Lua with tables, closures, string patterns and string.format, then run a JavaScript stand-in of the same design in your browser.
- Project: Inventory with Classes Build an inventory in Lua with a class, a subclass, metamethods for printing and comparing, sorting and a formatted report, with a JavaScript stand-in you can run.
- Project: A Text Adventure State Machine Build a text adventure in Lua: rooms as tables, a pattern-based command parser, a state machine and coroutines for conversations, with a JavaScript stand-in to run.
- Project: A Tiny Event Emitter and Scheduler Build an event emitter with closures and a cooperative scheduler that runs coroutines as timed tasks in Lua, with a JavaScript stand-in running a virtual clock.
Practice and Review
- Exercises with Solutions Fifteen graded Lua exercises on strings, tables, closures and metatables, each with a worked solution and a runnable JavaScript stand-in to check your thinking.
- Lua Interview Questions Common Lua interview questions with short answers: tables, nil vs false, 1-based indexing, closures, metatables, coroutines, errors and modules.
- Lua Cheat Sheet A Lua 5.4 cheat sheet: types, operators, control flow, functions, table and string library, metatables, coroutines and error handling at a glance.