Skip to content
Tutorials Center
My learning

Topics

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

  1. 1 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.
  2. 2 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.
  3. 3 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

  1. 4 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.
  2. 5 Strings Learn Lua strings: quote styles, long brackets, escapes, the .. concatenation operator, the # length operator, string.sub, upper, rep, format and immutability.
  3. 6 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

  1. 7 Conditionals Make decisions in Lua with if, elseif and else, nest conditions cleanly, and replace the missing switch statement with a table of functions.
  2. 8 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.
  3. 9 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

  1. 10 Functions Define and call Lua functions, pass parameters, return several values at once, understand value adjustment, and use the call syntax shortcuts.
  2. 11 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.
  3. 12 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

  1. 13 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.
  2. 14 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.
  3. 15 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

  1. 16 Metatables and Metamethods Understand Lua metatables: setmetatable, __index and __newindex for lookups and writes, arithmetic and comparison metamethods, __tostring, __call and __len.
  2. 17 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.
  3. 18 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

  1. 19 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.
  2. 20 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.
  3. 21 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

  1. 22 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.
  2. 23 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.
  3. 24 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

  1. 25 Lua in Games and Embedded Hosts See how Lua lives inside other programs: the C API idea, host functions, config scripts, LOVE and Roblox Luau differences, and the basics of sandboxing scripts.

Projects

  1. 26 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.
  2. 27 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.
  3. 28 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.
  4. 29 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

  1. 30 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.
  2. 31 Lua Interview Questions Common Lua interview questions with short answers: tables, nil vs false, 1-based indexing, closures, metatables, coroutines, errors and modules.
  3. 32 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.