Skip to content

Learn Lua

Lua brings modern scripting to Second Life using Luau, a high-performance typed variant of Lua developed by Roblox. If you’re familiar with Lua, Python, JavaScript, or other modern languages, you’ll feel right at home.

Lua offers several advantages for Second Life scripting:

  • Modern Language Features - coroutines, optional typing, objects, and more
  • Better Performance - Optimized runtime for complex operations
  • Familiar Syntax - If you know Lua, Python, or JavaScript, you’re already halfway there
  • Type Safety - Optional type annotations catch errors before runtime
  • Active Development - Regular improvements and new features

If you’re already familiar with LSL, Lua will feel familiar but more powerful. Here’s a quick comparison:

LSL
integer gIsRed = TRUE;
default {
touch_start(integer total_number) {
llSay(0, "Changing color!");
if (gIsRed) {
llSetColor(<0.0, 1.0, 0.0>, ALL_SIDES);
} else {
llSetColor(<1.0, 0.0, 0.0>, ALL_SIDES);
}
gIsRed = !gIsRed; // Toggle state
}
}
SLua
local isRed: boolean = true
function LLEvents.touch_start(detected: {DetectedEvent})
ll.Say(0, "Changing color!")
if isRed then
ll.SetColor(vector(0.0, 1.0, 0.0), ALL_SIDES)
else
ll.SetColor(vector(1.0, 0.0, 0.0), ALL_SIDES)
end
isRed = not isRed -- Toggle state
end

This guide will take you from Lua basics to advanced scripting:

Language Basics

Variables, types, functions, and control flow

Working with Events

Handle touch, collision, timer, and other events

Async Operations

Use coroutines for cleaner asynchronous code

Type System

Leverage optional types for safer code

Standard Library

Work with strings, tables, math, and more

Migration Guide

Convert your LSL scripts to Lua

Let’s write your first Lua script. This simple script makes an object say “Hello!” when touched:

-- Listen for touch events
function LLEvents.touch_start(detected: {DetectedEvent})
-- Say hello in local chat
ll.Say(0, "Hello, Avatar!")
end

That’s it! Save this script in a prim and touch it to see it work.

Here’s the recommended path through the Lua documentation:

  1. Language Fundamentals - (HELP WANTED) Start here if you’re new to Lua
  2. From LSL to Lua - Converting your LSL knowledge to Lua
  3. Events and Handlers - (HELP WANTED) How to respond to world events
  4. Async Programming - (HELP WANTED) Writing asynchronous code with coroutines
  5. Working with Types - (HELP WANTED) Using Lua’s optional type system
  6. Standard Library - (HELP WANTED) Complete reference of available functions