Creating an Object
Before you can write a script, you need something to put it in. In Second Life and OpenSim, everything in the world is built from prims — primitive shapes like boxes, spheres, and cylinders.
To create a prim, right-click the ground and select Create. Click once on the ground to place a basic cube. This is your object — and it will hold your first script.
You must be on land where you have build rights. Most sandboxes allow this freely. If you see "No Build" in the top bar, find a sandbox or your own land.
Once your cube is rezzed, right-click it and select Edit. In the Edit panel, click the Content tab. You'll see an empty list — this is where scripts and other items live inside your object.
The Default State
Click New Script in the Content tab. The Script Editor opens with a default script already written. Let's read it carefully:
default
{
state_entry()
{
llSay(0, "Hello, Avatar!");
}
}
The word default declares the script's first state. Every LSL script must have at least one state, and it must be called default.
Think of a state as a mode your script is in. A door script might have a default state (closed) and an open state. For now, we only need default.
- state
- A named block that groups related events together. Scripts can switch between states.
- default
- The mandatory starting state. Every script must have one.
state_entry Explained
Inside the default state, we have an event handler called state_entry(). This event fires in two situations:
- When the script starts for the first time
- When the script is reset
- When the script transitions into this state from another state
This makes it the perfect place for setup code — things you want to happen once when the script initialises: registering a listener, setting a timer, announcing that the object is ready.
default
{
state_entry()
{
// Everything here runs once on start/reset
llOwnerSay("Script initialised and ready.");
}
}
Lines starting with // are comments — notes for human readers that the script ignores completely.
llSay vs llOwnerSay
LSL has many built-in functions whose names start with ll (short for Linden Library). The two most basic are for sending chat messages:
- llSay(channel, message)
- Broadcasts publicly on the given channel. Channel 0 is the main chat everyone nearby can see.
- llOwnerSay(message)
- Sends a private message only the object's owner can see. Great for debugging and admin feedback.
default
{
state_entry()
{
// Everyone within 20m can see this
llSay(0, "Hello world!");
// Only you (the owner) can see this
llOwnerSay("Script started. Only you can read this.");
}
}
Use llOwnerSay while building and testing your scripts. Switch to llSay only when you want the public output. This keeps your testing noise-free.
Saving and Testing
When you click Save in the Script Editor, several things happen in order:
- The server compiles your script (checks it for errors)
- If compilation succeeds, the script is saved inside the object
- The script resets automatically —
state_entryfires - You see any output in your local chat
Try it now. Edit the default script so it says your name, then hit Save. You should see the message appear in chat almost instantly.
default
{
state_entry()
{
llOwnerSay("Script loaded! Welcome, scripter.");
}
touch_start(integer num_detected)
{
llSay(0, "You clicked me!");
}
}
Click Save, watch the chat, then click the object in-world. You should see "You clicked me!" appear.
Resetting a Script
Sometimes you need to reset a script — return it to its starting state, re-run state_entry, and clear all variable values. You have two options:
- Right-click the script → Reset
- Manual reset from the Content tab in the Edit panel
- llResetScript()
- The script resets itself from inside the code. Everything stops and starts fresh.
default
{
state_entry()
{
llOwnerSay("Script started.");
}
touch_start(integer num_detected)
{
llOwnerSay("Resetting now...");
llResetScript();
// Nothing after llResetScript() runs — the script stops here
}
}
When a script resets, all variable values are wiped back to their defaults. If you stored something important in a variable, it's gone after a reset.
Reading Common Errors
When you save a script with an error, the Script Editor highlights the problem and shows a message at the bottom. Here are the most common errors beginners see:
- Name not defined
- You used a variable or function name that doesn't exist. Check spelling — LSL is case-sensitive.
llsayis wrong;llSayis right. - Syntax error
- A structural problem — missing semicolon, unmatched curly brace, or typo in a keyword.
- Type mismatch
- You tried to put the wrong type of value somewhere — like passing a string where an integer is expected.
- Stack-heap collision
- You've run out of the script's memory (64KB in SL). Usually caused by very large lists or strings.
The most powerful debugging tool is llOwnerSay. Sprinkle it throughout your code to see what's happening at each step:
default
{
state_entry()
{
llOwnerSay("DEBUG: state_entry fired");
integer x = 10;
llOwnerSay("DEBUG: x = " + (string)x);
llOwnerSay("DEBUG: setup complete");
}
}
You're ready for Class 3. Next up: variables and data types — the building blocks of every script.