Class 2 of 8

Your First Script

Write, save, and test a working script from scratch.

Step 1 of 7
Step 1

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.

Tip

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.

Step 2

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:

LSL Default new script
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.

Key concept
state
A named block that groups related events together. Scripts can switch between states.
default
The mandatory starting state. Every script must have one.
Step 3

state_entry Explained

Inside the default state, we have an event handler called state_entry(). This event fires in two situations:

  1. When the script starts for the first time
  2. When the script is reset
  3. 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.

LSL state_entry for setup
default
{
    state_entry()
    {
        // Everything here runs once on start/reset
        llOwnerSay("Script initialised and ready.");
    }
}
Tip

Lines starting with // are comments — notes for human readers that the script ignores completely.

Step 4

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:

Chat functions
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.
LSL llSay vs llOwnerSay
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.");
    }
}
Best practice

Use llOwnerSay while building and testing your scripts. Switch to llSay only when you want the public output. This keeps your testing noise-free.

Step 5

Saving and Testing

When you click Save in the Script Editor, several things happen in order:

  1. The server compiles your script (checks it for errors)
  2. If compilation succeeds, the script is saved inside the object
  3. The script resets automatically — state_entry fires
  4. 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.

LSL Personalised greeting
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.

Step 6

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:

Reset methods
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.
LSL Self-resetting script
default
{
    state_entry()
    {
        llOwnerSay("Script started.");
    }

    touch_start(integer num_detected)
    {
        llOwnerSay("Resetting now...");
        llResetScript();
        // Nothing after llResetScript() runs — the script stops here
    }
}
Important

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.

Step 7

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:

Common errors
Name not defined
You used a variable or function name that doesn't exist. Check spelling — LSL is case-sensitive. llsay is wrong; llSay is 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:

LSL Debugging with llOwnerSay
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.