Lua lessons
Embedded scripting language
Lua is an extremely small and fast scripting language developed in Brazil in 1993. Its entire interpreter is just a few hundred kilobytes; therefore, it was designed to be embedded into other programs. Lua is not used to write a standalone application, but rather to extend an existing application with scripts.
Its most well-known use case is in games. All of Roblox's game logic is written in Lua; World of Warcraft add-ons, indie games made with Love2D, and the scripting layer of many game engines use Lua. Outside of gaming, Nginx configurations, Redis scripts, and Neovim plugins also use Lua.
The most characteristic feature of the language is having a single composite data structure: the table. An array is a table, a dictionary is a table, and an object is a table. This simplicity makes learning easy, but it requires a change in habit — additionally, array indices in Lua start at one, not zero.
- •→
Introduction to Lua
First step into the simple scripting language Lua: printing with print, variables, and concatenation with ..
- •→
Variables
Local variables with local, global variables, and nil.
- •→
Numbers & Math
Arithmetic (+ - * / % ^ //) and math library.
- •→
Strings
#s length, string.upper, string.sub (1-indexed) and string.find.
- •→
Tables
Lua's only data structure: arrays (1-indexed) and dictionaries; #t and table.insert.
- •→
Conditions (if)
if-elseif-else, then/end, and ~= (not equal).
- •→
Loops (for / while)
Numeric for (end INCLUSIVE), iterating tables with ipairs, and while.
- •→
Functions
Function definition, return, and multiple return values.
What you will learn
- Printing output with print and defining variables
- Using the table structure as an array and a dictionary
- Condition and loop structures
- Writing functions and returning multiple values
- Establishing object-like behavior with metatables
- Processing text with the string library
Who is this for?
- Those who want to develop games with Roblox or Love2D
- Developers who will write game or application add-ons
- Those who write Neovim, Nginx, or Redis scripts
When to use?
Lua is ideal as a scripting layer, game logic, add-on system, and configuration language to be embedded inside a game or application: it is small, fast, and easy to embed.
It is not suitable for a large standalone application, web backend, or data analysis; its standard library is intentionally very small and the ready-made package ecosystem is limited.
Frequently asked questions
Why do arrays in Lua start at 1?
It is a conscious choice by the designers; it matches mathematical and everyday counting habits. It is the most frequent source of errors for those coming from other languages: the first element is array[1], and the last element is array[length]. Loop boundaries must be set accordingly.
What is the difference between a table and an array?
There is no separate array type in Lua. When you put values into a table with numeric keys like 1, 2, 3, it behaves like an array; when you use text keys, it behaves like a dictionary. You can mix both in the same table, but the length calculation becomes unreliable then.
Is learning Lua enough for Roblox?
For the language side, yes. Roblox uses a Lua derivative called Roblox Luau, and the fundamentals are the same. However, to make games, you also need to learn Roblox Studio's object model, event system, and server-client separation. Mastering the language first makes this process much easier.
What is nil?
It is a special value indicating the absence of a value. When you read a variable you haven't defined, you get nil — it doesn't throw an error. This flexibility can turn into a silent bug when you misspell a variable name due to a typo; therefore, variable names need to be checked carefully.
Summary
Lua is popular as the scripting language for games and applications due to its small size and embeddability. In this tutorial, we start with print and progress to table structures, functions, and the use of metatables.