Introduction to Lua

Reviewed & published by Brayan K

Lua is a lightweight, fast, embeddable scripting language used to control and extend larger programs such as Roblox, Neovim, and Redis.

Part of the free Lua course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll know exactly what Lua is and why it powers Roblox, Neovim, and Redis — and you'll have written, commented, and run your own first Lua program.

What You'll Learn

💡 Real-World Analogy

Think of Lua as the engine you drop into someone else's car. Big programs — a game engine, a database, a text editor — are the car: the body, wheels, and dashboard. They handle the heavy machinery but need something small and nimble to tell them what to do. That's Lua. It's deliberately tiny so it can be embedded inside those bigger programs and "script" their behaviour. When you change a Roblox game's rules, configure Neovim, or speed up Redis, you're writing Lua that steers a much larger program — without rebuilding the whole car.

1. What Is Lua?

Lua (meaning "moon" in Portuguese, pronounced LOO-ah) is a lightweight, fast, embeddable scripting language created in 1993 at the Pontifical Catholic University of Rio de Janeiro. A scripting language is one you use to control or extend a program that already exists, rather than to build that program from the ground up. "Embeddable" means Lua is designed to live inside another application — it's tiny (the whole interpreter is a few hundred kilobytes) and one of the fastest scripting languages there is.

Because it's so small and quick, Lua became the go-to language for adding scripting to large software. You're almost certainly using something powered by Lua already:

Where Lua Runs in the Real World

The lesson: learning Lua isn't academic. The same print() and .. you're about to use are exactly what Roblox developers and Neovim users type every day.

2. Your First Program: print()

Lua has no boilerplate — no imports, no main function, no setup. A Lua program is just a text file (usually ending in .lua), and Lua runs it from the first line down. The one function you need on day one is print(), which displays whatever you give it on the screen. Read this worked example, then run it yourself.

-- Your very first Lua program.
-- "--" starts a comment: Lua ignores everything after it on the line.

print("Hello, World!")       -- print() shows text in the terminal

-- print() can take more than one value, separated by commas.
-- Lua puts a TAB between them.
print("Lua", "is", "fun")    -- prints: Lua	is	fun

-- prints:
-- Hello, World!
-- Lua	is	fun

Notice there's no ceremony: print("Hello, World!") on its own is a complete, runnable program. The text inside "double quotes" is called a string — that's just the programming word for "a piece of text".

3. Comments: Notes Lua Ignores

A comment is a note for humans that Lua skips over completely. You use comments to explain why code does something, or to temporarily switch a line off without deleting it. Lua has two kinds: a single-line comment starts with -- (two dashes) and runs to the end of the line, and a block comment is wrapped in --[[ and ]] and can span many lines.

-- A single-line comment starts with two dashes.
print("This line runs")      -- a comment can also sit after code

-- print("This line does NOT run") -- it's commented out

--[[
  This is a block (multi-line) comment.
  Everything between the double brackets is ignored,
  which is handy for notes or temporarily disabling code.
]]

print("Done")

-- prints:
-- This line runs
-- Done

4. Joining Text with ..

Often you want to build a message out of pieces — a fixed greeting plus a name, for example. Lua glues strings together with the concatenation operator .. (two dots with spaces around them). This is a key difference from many languages: in Lua, + is only for adding numbers, and .. is only for joining text. Here's a small but complete first program that uses a variable and concatenation together.

-- A tiny but complete program: greet a person by name.
-- "local" makes a variable that lives only in this file/block.
local name = "Ada"           -- a string: text wrapped in quotes

-- ".." is Lua's concatenation operator — it GLUES strings together.
-- (Most languages use "+", but in Lua "+" is only for numbers.)
print("Hello, " .. name .. "!")   -- prints: Hello, Ada!

-- You can also pass the pieces to print() with commas, but then
-- you get tabs between them instead of a clean join:
print("Hello,", name)        -- prints: Hello,	Ada

-- prints:
-- Hello, Ada!
-- Hello,	Ada

5. 🎯 Your Turn

Time to write some Lua yourself. Each program below is almost finished — replace the ___ blanks using the -- 👉 hints, then run it at onecompiler.com/lua and check your output against the -- ✅ Expected output comment.

-- 🎯 YOUR TURN — replace each ___ then run this at onecompiler.com/lua

-- 1) Print the exact text:  I am learning Lua
print(___)                   -- 👉 put the text in "double quotes"

-- 2) Print your favourite game on its own line
print(___)                   -- 👉 e.g. "Roblox"

-- ✅ Expected output (example):
-- I am learning Lua
-- Roblox

Next, practise the concatenation operator and a block comment in one go:

-- 🎯 YOUR TURN — build a greeting by gluing strings together.
local name = "Sam"

-- 1) Use .. to join "Hi, " then name then "!" into one line.
print("Hi, " ___ name ___ "!")   -- 👉 each ___ is the .. operator

-- 2) Add a block comment below describing what this program does.
___                              -- 👉 start with --[[ and end with ]]

-- ✅ Expected output:
-- Hi, Sam!

Pro Tip

Lua's entire standard library fits in your head — there are only about 30 built-in functions. That simplicity is a feature, not a limitation: it's exactly why Lua is so easy to learn and so quick for big programs to embed.

Common Errors (and the fix)

Quick Reference

TaskCodeResult
Print textprint("Hello")Hello
Print several valuesprint("a", "b")a b
Single-line comment-- note here(ignored)
Block comment--[[ ... ]](ignored)
Join text"a" .. "b"ab
Local variablelocal name = "Ada"Ada

Mini-Challenge: Your Business Card

No blanks this time — just a brief and an outline to keep you on track. Write the whole thing yourself, run it, and check your output against the example in the comments. This is the kind of tiny, self-contained script real projects are full of.

-- 🎯 MINI-CHALLENGE: a one-line "business card"
-- 1. Make a local variable "name"  (your name, in quotes)
-- 2. Make a local variable "role"  (e.g. "Lua Learner")
-- 3. Use print() and .. to show:   name .. " — " .. role
-- 4. BONUS: add a block comment at the top explaining the script
--
-- ✅ Expected output (example):
-- Ada — Lua Learner

-- your code here

🎉 Lesson Complete

Practice quiz

How do you display text in the console in Lua?

  • print("Hi")
  • echo("Hi")
  • console.log("Hi")
  • write("Hi")

Answer: print("Hi"). print() is the built-in function that shows values on screen.

Which operator joins (concatenates) two strings in Lua?

  • +
  • ..
  • &
  • concat

Answer: ... Lua uses .. (two dots) to concatenate; + is only for numbers.

How do you start a single-line comment in Lua?

  • //
  • #
  • --
  • /*

Answer: --. Two dashes -- begin a single-line comment in Lua.

Which keyword makes a variable local to its block?

  • var
  • let
  • const
  • local

Answer: local. local declares a block-scoped variable; without it the variable is global.

What does print("a", "b") put between the values?

  • A tab character
  • A space
  • A comma
  • Nothing

Answer: A tab character. print separates multiple arguments with a tab.

What happens if you write name = "Sam" without local?

  • A syntax error
  • It creates a global variable
  • It does nothing
  • It creates a constant

Answer: It creates a global variable. Bare assignment creates a global variable in Lua.

How do you write a block (multi-line) comment in Lua?

  • /* ... */
  • ### ... ###
  • --[[ ... ]]
  • <!-- ... -->

Answer: --[[ ... ]]. Block comments are wrapped in --[[ and ]].

What does "Lua" mean in Portuguese?

  • Light
  • Fast
  • Script
  • Moon

Answer: Moon. Lua means moon in Portuguese.

Why does print("Hi " + name) fail in Lua?

  • + is only for numbers, not joining text
  • name is undefined
  • print takes one argument
  • Strings need single quotes

Answer: + is only for numbers, not joining text. In Lua + does arithmetic; joining text needs .. instead.

Which Roblox dialect of Lua powers Roblox game logic?

  • LuaJIT
  • Luau
  • MoonScript
  • Terra

Answer: Luau. Roblox uses Luau, its own dialect of Lua.

Continue this course

Frequently asked questions

Is Lua hard to learn for a complete beginner?

Lua is one of the easiest languages to start with. The whole language is tiny — there are only about 30 built-in functions and a handful of keywords — so you can hold most of it in your head. You'll write a working program in your first hour.

Do I need to install anything to run Lua?

Not necessarily. You can run every example in this lesson for free in your browser at onecompiler.com/lua or lua.org/demo. When you're ready, install Lua locally from lua.org/download and run scripts with the 'lua' command.

Why does Lua use .. for joining text instead of +?

In Lua, + is strictly for arithmetic on numbers, and .. (two dots) is strictly for concatenating strings. Keeping them separate avoids the confusion some languages have where + means both 'add' and 'join' depending on the values.

Is the Lua I learn here the same as Roblox's Luau?

Yes — Luau is Roblox's dialect of Lua. Everything in this lesson (print, comments, variables, concatenation) works identically in Luau. Luau just adds extra features like type annotations on top, so what you learn here transfers directly.