Introduction to Programming in Lua

Lua is a lightweight, embeddable scripting language known for its simplicity, speed, and flexibility. It is widely used in game development (e.g., Roblox, World of Warcraft), embedded systems, and as a general-purpose scripting language. Lua’s minimalistic design makes it easy to learn, while its powerful features like tables and coroutines make it highly versatile.

This guide will introduce you to the fundamentals of Lua programming, helping you write clean and efficient scripts for a variety of applications.


Table of Contents

  1. What is Lua?
  2. Setting Up Your Environment
  3. Variables and Data Types
  4. Operators and Expressions
  5. Conditional Statements
  6. Loops
  7. Functions
  8. Tables
  9. Strings
  10. Coroutines

What is Lua?

Lua is a lightweight, high-level scripting language designed for embedded systems and extensibility. It is often used as a scripting language in games, applications, and tools.

-- Example: Your first Lua program
print("Hello, Lua Programming!")

Setting Up Your Environment

To write and run Lua programs, you need the Lua interpreter installed on your system. You can download it from the official Lua website.

# Example: Running a Lua script
lua script.lua

Variables and Data Types

Lua is dynamically typed, meaning you don’t need to declare variable types explicitly. Common data types include nil, boolean, number, string, and table.

-- Example: Declaring variables
local age = 25
local height = 5.9
local name = "Alice"
local isStudent = true

print("Name:", name, "Age:", age, "Height:", height)

Operators and Expressions

Lua includes arithmetic, relational, and logical operators to perform operations on data.

-- Example: Using operators
local a = 10
local b = 5
local sum = a + b -- 15
local isGreater = a > b -- true

print("Sum:", sum, "Is Greater:", isGreater)

Conditional Statements

Conditional statements like if, elseif, and else allow you to execute code based on certain conditions.

-- Example: Conditional statement
local age = 18

if age >= 18 then
    print("You are an adult.")
else
    print("You are a minor.")
end

Loops

Lua supports for, while, and repeat-until loops for repeating blocks of code.

-- Example: For loop
for i = 1, 5 do
    print("Iteration:", i)
end

Functions

Functions are reusable blocks of code that perform a specific task. You can define functions using the function keyword.

-- Example: Function
local function add(a, b)
    return a + b
end

local result = add(5, 10)
print("Result:", result) -- 15

Tables

Tables are Lua’s primary data structure, used to represent arrays, dictionaries, and more.

-- Example: Table
local person = {
    name = "Alice",
    age = 25,
    isStudent = true
}

print("Name:", person.name, "Age:", person.age)

Strings

Lua provides powerful string manipulation capabilities, including pattern matching and concatenation.

-- Example: String manipulation
local greeting = "Hello"
local name = "Alice"
local message = greeting .. ", " .. name .. "!"

print(message) -- Hello, Alice!

Coroutines

Coroutines are a unique feature of Lua that allow cooperative multitasking. They enable you to pause and resume functions.

-- Example: Coroutine
local function task()
    print("Task started")
    coroutine.yield()
    print("Task resumed")
end

local co = coroutine.create(task)
coroutine.resume(co) -- Task started
coroutine.resume(co) -- Task resumed