Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 32 ยท 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.

Lua is a small scripting language that was designed to be embedded inside other programs. It was created in 1993 at the Pontifical Catholic University of Rio de Janeiro in Brazil, and its name is the Portuguese word for "moon". The whole language is tiny: a handful of statements, eight basic types and one data structure, the table. Because it is so small, it is easy to learn in a weekend and easy for other programs to carry around.

You have probably used Lua without noticing. Game engines let designers script enemies and menus in it, Redis runs Lua scripts atomically on the server, Neovim uses it for configuration and plugins, and many network devices, routers and media players use it for settings and automation. In this tutorial you will learn the language itself (Lua 5.4), then tables, metatables, coroutines and the standard library, and finish with four small projects.

What makes Lua different

Lua's authors chose a few goals and stuck to them for thirty years.

Design goals of Lua

Small The whole interpreter is a few hundred kilobytes.
Fast Compiled to bytecode for a quick virtual machine.
Embeddable Other programs host it and talk to it through a C API.
Simple Few concepts, combined in flexible ways.
1 / 2

Other languages ship dozens of built-in collection types and a large standard library. Lua gives you one flexible building block and lets you build the rest. Instead of adding classes, it gives you tables and metatables (covered in Metatables and Metamethods). Instead of adding threads, it gives you coroutines (see Coroutines). This "mechanisms, not policies" approach is why the language has stayed small.

A first look at the code

Here is a taste of real Lua. These blocks are read-only: the site cannot run Lua itself, but every lesson shows the exact code you would run on your own machine and then gives you a JavaScript stand-in that you can edit here.

LUA
-- a comment starts with two dashes
local name = "Moon"
local count = 3

for i = 1, count do
  print("Hello, " .. name .. " number " .. i)
end

local planets = { "Mercury", "Venus", "Earth" }
print(#planets, planets[1])   --> 3   Mercury

A few things to notice. Variables are declared with local. Strings are joined with ... A for loop ends with end instead of curly braces. And the first element of a list is planets[1], not planets[0]: Lua counts from 1. All of this is explained step by step in the next lessons.

Where Lua is used

Area Example What Lua does there
Games LOVE, many commercial engines, Roblox (with its own dialect) Gameplay scripts, UI, level logic
Databases Redis EVAL Runs a script atomically next to the data
Editors Neovim Configuration and plugins
Web servers OpenResty (Nginx with Lua) Request handling, routing, filtering
Devices Routers, TVs, packet analyzers Settings, automation, extensions

The common thread is that a larger program, the host, wants a safe, small language for the people who customize it. The host does the heavy lifting in C, C++ or another language and exposes a few functions to Lua. A game designer can then change behavior without recompiling the game. You will see this idea in Lua in Games and Embedded Hosts.

Lua 5.4 versus LuaJIT

When you read about Lua you will meet more than one version, so it helps to know the families.

  • Lua 5.4 is the reference implementation from PUC-Rio and the version this tutorial teaches. It has separate integer and floating-point number subtypes, the // floor-division operator, bitwise operators, goto, and <const> and <close> variable attributes.
  • LuaJIT is a separate, very fast implementation with a just-in-time compiler. It follows the older Lua 5.1 language with a few extras, so it has no integer subtype and no // operator.
  • Lua 5.1 flavors live inside many hosts. Redis scripts, for example, use a 5.1-style Lua, and Neovim uses LuaJIT on most builds.
  • Luau (Roblox) is a descendant with its own type syntax and extra features.

The core of the language is the same in all of them: tables, functions, closures, metatables and coroutines. When a lesson shows a feature that only exists in 5.3 or later, it says so. If you write code for a specific host, check which version it embeds.

Running Lua on your own machine

To follow along with real Lua, install Lua 5.4 from your package manager or from the official download page, then save a file and run it with the interpreter.

Terminal
lua hello.lua      # run a file
lua                # start the interactive prompt (REPL)
lua -e "print(2 + 3)"   # run one line

The interpreter is just a program that reads text, compiles it to bytecode and runs it. There is no project setup, no build step and no package manager required to start. The next lesson, Your First Lua Program, walks through writing and running a file.

Stand-ins on this site

The browser cannot run a Lua interpreter here, so every runnable example in this tutorial is a JavaScript stand-in. A stand-in models a Lua idea (such as a table or a host function) in JavaScript so you can experiment with the behavior. The text always says which part is real Lua and which part is the stand-in. The first stand-in shows Lua's most important data structure: a table that plays several roles at once.

JS One table, three roles (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The second stand-in models the host idea. A host program builds an environment with a few functions and then runs a script that can only use what it was given.

JS A host exposes functions to a script (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

In real embedding, the host is written in C or another language and registers its functions with the Lua state; the script never sees anything else. That is also why Lua is popular for safe customization.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the word Lua mean?

    Lua is Portuguese for moon. The language was created at PUC-Rio in Brazil.
  2. 2. Which data structure is Lua's main building block?

    Tables serve as arrays, dictionaries, objects and modules. Almost everything else is built from them.
  3. 3. Which statement about Lua 5.4 and LuaJIT is correct?

    LuaJIT is an independent, fast implementation based on the Lua 5.1 language. Lua 5.4 is the PUC-Rio reference implementation.
  4. 4. Why is Lua popular inside games and servers?

    A host can embed the small interpreter, expose chosen functions and let users customize behavior safely.

Summary

  • Lua is a small, fast, embeddable scripting language from Brazil; its name means "moon".
  • Tables are the single data structure behind arrays, dictionaries, objects and modules.
  • It powers game scripts, Redis scripts, Neovim configuration, OpenResty and many devices.
  • Lua 5.4 is the reference version taught here; LuaJIT follows Lua 5.1 and is separate.
  • Scripts run with the lua command. On this site, real Lua is read-only and the runnable examples are JavaScript stand-ins.

Next: write and run a script in Your First Lua Program.