Intro to Lua

Tiny, fast, and powerful. The secret weapon behind your favorite games.

1

The Story of Lua

In 1993, three computer scientists at PUC-Rio university in Brazil created a language that would quietly become one of the most widely embedded languages in the world. They called it "Lua," the Portuguese word for "moon."

1993
Lua is born at PUC-Rio in Rio de Janeiro, Brazil. Roberto Ierusalimschy, Waldemar Celes, and Luiz Henrique de Figueiredo created it because Brazil had strict trade restrictions on importing software. They needed to build their own tools.
2003
Lua enters gaming. World of Warcraft adopts Lua for its entire addon and UI scripting system. Millions of players start writing Lua without even knowing it.
2005
LuaJIT arrives. Mike Pall creates a just-in-time compiler for Lua, making it one of the fastest dynamic language runtimes ever built.
2006
Roblox launches with Lua as its scripting language. Today, tens of millions of young developers write Lua to build Roblox games.
Today
Lua is everywhere: game engines, routers, embedded devices, Angry Birds, Adobe Lightroom, Redis, Neovim, and thousands of other programs use Lua as their scripting layer.
</>
Roblox
All game scripts
PvP
World of Warcraft
UI and addons
Lr
Adobe Lightroom
Plugin system
AB
Angry Birds
Game logic
vim
Neovim
Config and plugins
DB
Redis
Server-side scripts
Fun Fact

Lua was not Brazil's first homegrown language. The same team created two earlier languages called SOL ("Simple Object Language") and DEL ("Data Entry Language"). SOL is also Portuguese for "sun." From the sun came the moon: Lua.

2

Hello, World!

Lua's syntax is clean and readable. No semicolons, no curly braces, no type declarations. You just write what you mean.

Printing output

print("Hello, World!")

That is the entire program. One line. print() sends text to the output.

Variables

In Lua, you do not need a keyword like var or let. Just assign a value to a name.

-- Variables: no declaration keyword needed name = "Lua" year = 1993 is_cool = true print(name) -- Lua print(year) -- 1993 print(is_cool) -- true

String concatenation

Lua uses .. to join strings (not +).

greeting = "Hello, " .. "World!" print(greeting) -- Hello, World! -- Numbers convert automatically print("Lua was born in " .. 1993) -- Lua was born in 1993

Try it in the REPL

Type lua in the terminal to open the interactive prompt. Type code, press Enter, see the result.

> print("Hello!") Hello! > x = 10 + 5 > print(x) 15 > print("Result: " .. x) Result: 15
3

Tables: Lua's Secret Weapon

Most languages have arrays, dictionaries, objects, and classes as separate things. Lua has one data structure that does all of it: the table. If you understand tables, you understand Lua.

Tables as arrays

Use curly braces to create a table. Lua arrays start at index 1, not 0.

-- A simple list (array) colors = {"red", "green", "blue"} print(colors[1]) -- red (indexing starts at 1!) print(colors[2]) -- green print(#colors) -- 3 (# gives the length)

Tables as dictionaries

Add named keys and the table becomes a key-value store, like a dictionary or object.

-- A key-value table (like an object) player = { name = "Luna", health = 100, level = 5, alive = true } print(player.name) -- Luna print(player.health) -- 100 print(player["level"]) -- 5 (bracket syntax works too)

Nested tables

Tables inside tables. You can build any data structure you need.

-- An inventory system inventory = { {name = "Sword", damage = 25}, {name = "Shield", defense = 15}, {name = "Potion", healing = 50} } print(inventory[1].name) -- Sword print(inventory[1].damage) -- 25
Key Insight

In Lua, everything is a table. Arrays are tables with integer keys. Objects are tables with string keys. Modules are tables. Even the global scope (_G) is a table. This simplicity is by design: one powerful concept instead of a dozen specialized ones.

4

Functions and Closures

Lua functions are first-class values. You can store them in variables, pass them as arguments, and return them from other functions, just like numbers or strings.

Basic functions

function greet(name) return "Hello, " .. name .. "!" end print(greet("World")) -- Hello, World!

Multiple return values

Most languages only let a function return one thing. Lua functions can return as many values as you want.

function getPosition() return 10, 20 end x, y = getPosition() print(x) -- 10 print(y) -- 20

Functions as values

Functions are values in Lua, just like strings or numbers. Store them in variables or tables.

-- Store a function in a variable square = function(x) return x * x end print(square(5)) -- 25 -- Store functions in a table math_ops = { add = function(a, b) return a + b end, mul = function(a, b) return a * b end } print(math_ops.add(3, 4)) -- 7

Closures

A closure is a function that remembers values from the scope where it was created, even after that scope is gone.

function makeCounter() local count = 0 return function() count = count + 1 return count end end counter = makeCounter() print(counter()) -- 1 print(counter()) -- 2 print(counter()) -- 3 -- The 'count' variable lives on inside the closure
5

What Makes Lua Special

Plenty of scripting languages exist. Here is why Lua keeps showing up in places that matter.

Incredibly Small

The entire Lua interpreter is roughly 200KB. That is smaller than most image files. You can embed it in a microcontroller, a game console, or a router.

Blazing Fast

LuaJIT, the just-in-time compiled version, is one of the fastest dynamic language runtimes in existence. It regularly outperforms Python, Ruby, and JavaScript in benchmarks.

Embeddable by Design

Lua was built to live inside other programs. Its C API makes it trivial to add scripting to any C or C++ application. That is why game engines love it.

One Data Structure

Tables handle arrays, dictionaries, objects, classes, and modules. Instead of learning five different container types, you learn one flexible concept.

Simple Syntax

Lua has only 21 reserved keywords. Compare that to Java (50+), C++ (80+), or even Python (35). The entire language reference fits in a small booklet.

Battle-Tested

Roblox, World of Warcraft, Adobe Lightroom, Angry Birds, Nginx (OpenResty), Redis, Wireshark, and hundreds of game engines all depend on Lua in production.

Comparison

Lua's entire source code is about 30,000 lines of C. Python's is over 400,000. JavaScript (V8 engine) is over 1,000,000. Lua proves that a language does not need to be large to be powerful.

6

Try It Yourself

Open the terminal and start writing Lua right now. Here is everything you need.

Quick start

1
Install Lua
apt install lua
2
Create a file
nano game.lua
3
Run it
lua game.lua

Mini-project: text adventure

Copy this into game.lua and run it. Then modify it to add your own rooms and choices.

Text Adventure Starter

-- A tiny text adventure in Lua print("You wake up in a dark cave.") print("There are two tunnels: left and right.") print("") io.write("Which way? (left/right): ") choice = io.read() if choice == "left" then print("You find a chest full of gold!") print("You win!") elseif choice == "right" then print("A dragon blocks your path!") print("Game over.") else print("You stand still. Nothing happens.") end

Challenge: Add more rooms. Use tables to store room descriptions and connections. Add an inventory system with a table. Make the dragon fight winnable if the player has a sword.

Tip

You can also use the Lua REPL for quick experiments. Just type lua with no arguments to start it. Try print(type({})) to see what type a table is, or print(type(print)) to confirm that functions are values.

Lua Scripter!

You've explored one of the most elegant scripting languages ever created. From Roblox to game engines to embedded systems, Lua is everywhere. Now you know why.

0
Sections Explored
0
Time Exploring

Born in Brazil

Lua was created in 1993 at PUC-Rio in Brazil. "Lua" means "moon" in Portuguese. It was built because trade restrictions made importing software impossible.

Tables Do Everything

Lua has one data structure: the table. It works as an array, dictionary, object, module, and namespace. Simple by design, powerful in practice.

Made to Be Embedded

At roughly 200KB, Lua is designed to live inside other programs. Game engines, routers, Adobe Lightroom, and Redis all use Lua as their scripting layer.

Functions Are First-Class

Lua treats functions like any other value. You can store them in variables, pass them as arguments, and return them from other functions.

Ready to Create?

Put your new knowledge into practice!

Suggest a Correction