Learn Lua

Tiny, fast embeddable scripting language for games, editors and config

16 lessons · 3 hours · 4 levels · covers Lua 5.4

Lua is a small, fast, dynamically typed scripting language designed to be embedded in larger programs. It is the scripting layer of game engines and platforms like Roblox (Luau), LÖVE and World of Warcraft add-ons, of Neovim, Redis, Nginx/OpenResty and many network devices. Its whole design rests on a few powerful ideas (tables, first-class functions, metatables and coroutines), so it is quick to learn and teaches you a lot about how dynamic languages work.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics5 lessons · 50 min in total
2. PracticalUse it productively at work, with the usual idioms10 lessons · 2 hours in total
3. AdvancedThe less common features, performance and trade-offs13 lessons · 2 hours in total
4. ExpertInternals and the edge cases experts know16 lessons · 3 hours in total

The Lua tree

LuaRunning LuaRunning LuaValues, types and variablesValues, types andvariablesConditions and loopsConditions and loopsTables: arrays and dictionariesTables: arrays anddictionariesFunctions and multiple returnsFunctions andmultiple returnsStrings and patternsStrings and patternsIterators: pairs, ipairs and generic forIterators: pairs, ipairsand generic forClosures and upvaluesClosures andupvaluesModules and requireModules and requireErrors, pcall and assertErrors, pcall andassert
Essentials and Practical shown (10 of 16 lessons); the full list is below. Click a lesson to read it.

All 16 lessons, in order

Basics

  1. Running Lua · Essentials · 6 min

    Run Lua scripts and the interactive interpreter, print values and write comments.

  2. Values, types and variables · Essentials · 10 min

    Lua's eight types, integer versus float numbers, and why you should declare variables local.

  3. Conditions and loops · Essentials · 10 min

    if/elseif, while, repeat-until and the numeric for loop, plus Lua's truthiness rule.

Tables and functions

  1. Tables: arrays and dictionaries · Essentials · 12 min

    The table is Lua's only data structure: use it as an array, a dictionary, an object or a module.

  2. Functions and multiple returns · Essentials · 12 min

    Define functions, return several values, accept variable arguments and pass functions around.

Idiomatic Lua

  1. Strings and patterns · Practical · 14 min

    Format, search and transform text with the string library and Lua patterns.

  2. Iterators: pairs, ipairs and generic for · Practical · 10 min

    Loop over tables with pairs and ipairs, and write your own iterator functions.

  3. Closures and upvalues · Practical · 10 min

    Functions capture the local variables around them, giving you private state without classes.

  4. Modules and require · Practical · 10 min

    Split code into files that return a table, and load them with require.

  5. Errors, pcall and assert · Practical · 10 min

    Raise errors with error and assert, and catch them with pcall and xpcall.

Metatables and coroutines

  1. Metatables and metamethods · Advanced · 14 min

    Customise how tables behave for indexing, arithmetic, comparison, calling and printing.

  2. Classes and inheritance with metatables · Advanced · 14 min

    Build classes, methods and single inheritance from tables, __index and the colon syntax.

  3. Coroutines · Advanced · 14 min

    Suspend and resume functions with coroutine.yield and resume to write generators and cooperative tasks.

Runtime and embedding

  1. Garbage collection and weak tables · Expert · 14 min

    How Lua's incremental and generational collectors work, and how weak tables and __gc interact with them.

  2. Embedding Lua: the C API · Expert · 18 min

    Embed the interpreter in a C program and expose C functions through Lua's virtual stack.

  3. Performance and table internals · Expert · 14 min

    How tables store array and hash parts, why locals are fast, and the habits that make Lua code quick.