Your First Scripted Object

Make a box in World, give it a script, and change its colour when touched.

You need a place where you can build and an object you can edit. Start with one plain, unlinked box. A prim is a basic building shape; its Contents hold scripts and other assets. A script is saved text that the runtime turns into behaviour on that object.

1. Create a box

  1. Open World and choose Build.
  2. In the shape controls, choose Box under Create. Click in the world to place it.
  3. Select your box. Give it enough space to see and touch it; leave its click action unchanged for this exercise.

Use the box's own Contents throughout this lesson. A script placed in a different object will act on that object instead.

2. Add an LSL script

  1. With the box selected, open the Code tab and find Contents.
  2. Choose the button whose tooltip is New Script, then choose LSL Script. The editor opens with LSL on one side and generated TypeScript on the other.
  3. Replace the LSL source with the example below.
  4. Choose Transpile or press Ctrl+S. Fix any compiler errors, and wait for the object's save indicator to confirm the save. A refused or failed save needs resolving before you continue.
default
{
    state_entry()
    {
        llOwnerSay("Ready. Touch this box.");
    }

    touch_start(integer count)
    {
        llOwnerSay("Hello, " + llDetectedName(0) + "!");
    }
}

state_entry runs when the script enters its default state. touch_start responds to a touch. llDetectedName(0) reads the first detected person's name, and llOwnerSay sends the message to the object's owner.

3. Run it on the box

  1. Close the script editor to return to Contents.
  2. On your script's row, choose the play control whose tooltip is Run script. A successful start returns you to gameplay.
  3. Look for the owner's chat message Ready. Touch this box.
  4. Touch the box in gameplay. You should receive a greeting with your name.
Selecting and touching are different actions

While the build tool owns the pointer, clicking selects or edits objects. Return to gameplay to test a script touch. The object's explicit Touch menu command is another way to invoke it.

The standalone Scripter editor is useful for writing and transpiling. Its preview Run button runs in the preview environment. Use the script's Contents row in World to run it on the box you placed.

4. Make the box change colour

Return to Build and select the same box. Stop its script, open that script for editing, and replace its LSL source with this example. Transpile and confirm the save, then use the Contents row to run it again.

Download the colour-toggle script

integer warm = FALSE;

default
{
    state_entry()
    {
        llSetColor(<0.2, 0.5, 1.0>, ALL_SIDES);
        llOwnerSay("Ready. Touch to switch between blue and orange.");
    }

    touch_start(integer count)
    {
        warm = !warm;
        if (warm)
        {
            llSetColor(<1.0, 0.4, 0.1>, ALL_SIDES);
            llOwnerSay("Orange.");
        }
        else
        {
            llSetColor(<0.2, 0.5, 1.0>, ALL_SIDES);
            llOwnerSay("Blue.");
        }
    }
}

The warm variable remembers which colour comes next while this execution runs. A colour vector contains red, green and blue values from 0 to 1. ALL_SIDES applies the colour to every face of the box. You should see blue at startup, orange on the first touch, and blue on the next.

Try changing one colour vector, then save and run the revised source. To finish, use Stop script in Contents. Further touches should produce no new messages or colour changes from this script.

5. Respond to pressing, holding and releasing

A button can react when you press it, while you hold it, and when you release it. LSL provides three events for those phases:

Download the press/hold/release script. On your plain box, replace the lesson script with this file's contents, then save and Stop/Run that script as described above. Leave Build mode and look for Ready: press, hold, release.

  1. Click and release quickly. Expect start, then end. A short click may finish before any held callback occurs.
  2. Hold for about two seconds, then release. Expect start, sampled held messages, and one end with the final callback count. No further held messages should follow.
  3. Hold, move off the box, return, then release. World ends this gesture when you leave the box. Returning while still pressed should not start it again; release should not print a second end.

The script prints the first held callback and every tenth afterward to keep chat readable. That count measures callbacks, not seconds. Use elapsed time if your own script needs a duration.

For a release-only button, put the touch-end-only script on a second box. Each click/release should print once. The object's explicit Touch menu action should also print once; selecting the box in Build mode should print nothing.

These examples only send chat and request no avatar permissions. Stop script stops that script's execution. The in-world consent Stop control governs avatar permissions and effects; use Events & Permissions when building an object that animates or attaches to an avatar.

If something does not happen

Check scripts in Contents checks saved source and compiler information. It does not run the lesson or prove that the behaviour works. After changing source, confirm the save before loading the revised script from its Contents row.

What to learn next