Examples

Short, working scripts to copy. Each is a --!server script that finds the parts it needs by name, so make parts with those names in Polycarbonate (or change the names). Most of them are also offered as starters in Polycarbonate.

A kill brick

--!server
local hazard = world.root:find("Hazard")

hazard.stepped_on:connect(function(player)
    player.health = 0
    world:announce(player.name .. " hit a hazard!")
end)

A teleporter

--!server
local pad = world.root:find("Pad")
local target = world.root:find("Target")

pad.stepped_on:connect(function(player)
    player:teleport(target.position + vec(0, 4, 0))
end)

A sound on a pad

--!server
-- Replace the number with the id of a sound you uploaded.
local pad = world.root:find("Pad")
local sound = Sound.new { asset = "sound/1", parent = pad }

pad.stepped_on:connect(function()
    sound:play()
end)

Music for everyone

--!server
local music = Sound.new { asset = "sound/1", looped = true, volume = 0.5 }
music:play()

A welcome message

--!server
local function greet(player)
    player:tell("Welcome, " .. player.name .. "!")
end

world.joined:connect(greet)
for _, player in world:query { class = Player }:entities() do
    greet(player)
end

A spinning part

--!server
local spinner = world.root:find("Spinner")

world.frame:connect(function(dt)
    spinner.rotation += vec(0, 90 * dt, 0)    -- 90 degrees a second
end)

A platform that gives way

The timers are in the script's scope, so they end with it.

--!server
local platform = world.root:find("Platform")
local waiting = false

platform.stepped_on:connect(function()
    if waiting then
        return
    end
    waiting = true
    scope:after(1, function()
        platform.solid = false
        platform.transparency = 0.8
        scope:after(3, function()
            platform.solid = true
            platform.transparency = 0
            waiting = false
        end)
    end)
end)

A button and a door

--!server
local button = world.root:find("Button")
local door = world.root:find("Door")

button.clicked:connect(function(player)
    door.solid = not door.solid
    door.transparency = door.solid and 0 or 0.7
end)

A coin spawner

--!server
local spawner = world.root:find("Spawner")
local count = 0

scope:every(2, function()
    count += 1
    local coin = Part.new {
        name = "Coin" .. count,
        position = spawner.position + vec(0, 6, 0),
        size = vec(1.5, 0.4, 1.5),
        color = rgb(255, 200, 0),
        anchored = false,
    }
    scope:after(5, function()
        coin:destroy()
    end)
end)

A rainbow

--!server
local rainbow = world.root:find("Rainbow")

world.frame:connect(function()
    local t = time()
    rainbow.color = vec(0.5 + 0.5 * math.sin(t), 0.5 + 0.5 * math.sin(t + 2.1), 0.5 + 0.5 * math.sin(t + 4.2))
end)

Acting on many things with one call

--!server
local center = world.root:find("Center")

scope:after(1, function()
    -- One query, one call: no loop over the parts.
    world:query { class = Part, within = { center = center.position, radius = 20 } }:set("transparency", 0.5)
end)

A countdown for everyone

--!server
scope:spawn(function()
    for seconds = 5, 1, -1 do
        world:announce("Starting in " .. seconds)
        sleep(1)
    end
    world:announce("Go!")
end)

A round that cleans up after itself

Everything started in the round's scope ends with it.

--!server
local function playRound()
    local round = scope:child()

    round:every(1, function()
        print("tick")
    end)
    round:defer(function()
        world:announce("The round is over.")
    end)

    scope:after(10, function()
        round:cancel()       -- the ticking stops, the deferred function runs
    end)
end

playRound()

Waiting for a player to do something

--!server
local door = world.root:find("Door")

scope:spawn(function()
    local who = door.touched:wait()
    print("opened by", who.name)
end)

Whichever happens first

--!server
local button = world.root:find("Button")

scope:spawn(function()
    local index = scope:race {
        function() button.clicked:wait() return "clicked" end,
        function() sleep(30) return "timed out" end,
    }
    print(index == 1 and "someone clicked" or "nobody clicked in 30 seconds")
end)

A fading door

tween moves a property over time, in the part's own scope.

--!server
local door = world.root:find("Door")

door.clicked:connect(function()
    door.solid = not door.solid
    door:tween({ transparency = door.solid and 0 or 0.7 }, 0.3, "out")
end)

One behavior, many doors

A behavior is written once and attached to every part that needs it. Each gets its own parameters. See Components, behaviors and modules.

--!server
local Door = behavior("Door", {
    params = {
        open_transparency = { default = 0.7, min = 0, max = 1 },
        seconds = { default = 0.3, min = 0 },
    },
    start = function(self)
        self.open = false
        self.entity.clicked:connect(function()
            self:toggle()
        end)
    end,
    toggle = function(self)
        self.open = not self.open
        self.entity.solid = not self.open
        self.entity:tween({ transparency = self.open and self.params.open_transparency or 0 }, self.params.seconds)
    end,
})

world:query { class = Part, where = function(part) return part.name == "Door" end }:each(function(door)
    door:attach(Door)
end)

Health as a component

--!server
local Health = component("Health", { hp = 100 })
local Boss = component("Boss")

local grunt = Part.new { name = "Grunt" }
grunt:add(Health)
local boss = Part.new { name = "Boss" }
boss:add(Health, { hp = 1000 })
boss:add(Boss)

-- Hurt everything that has health and is not a boss, in one pass.
scope:every(1, function()
    world:query { has = { Health }, without = { Boss } }:each(function(e)
        e:get(Health).hp -= 5
    end)
end)

Counting visits with saved data

The script waits while the answer comes back; nothing else does. See Saved data.

--!server
local visits = store("Visits")

local function count(player)
    local n = visits:increment(player.user_id)
    player:tell("Welcome! This is visit number " .. n .. ".")
end

world.joined:connect(count)
for _, player in world:query { class = Player }:entities() do
    count(player)
end

A line of sight

--!server
local turret = world.root:find("Turret")
local lamp = world.root:find("Lamp")

scope:every(0.2, function()
    local hit = world:raycast { origin = turret.position, direction = vec(0, 0, 1), max = 50, ignore = turret }
    lamp.color = hit and rgb(255, 0, 0) or rgb(0, 255, 0)
end)

A library

--!module
-- A script named "Util"
local Util = {}
function Util.clamp01(x)
    return math.max(0, math.min(1, x))
end
return Util
--!server
local Util = require("Util")
print(Util.clamp01(1.5))

A button that asks the server for something

The client draws the button and asks. The server decides. See Client scripts and interface.

--!shared
local Buy = net.message("buy", { item = "string" })
local Bought = net.message("bought", { item = "string" })
--!server
-- Put the two lines above in a --!shared script named "Messages", or repeat them here.
local Buy = net.message("buy", { item = "string" })
local Bought = net.message("bought", { item = "string" })
local prices = { sword = 10, shield = 8 }

Buy:listen(function(player, data)
    if prices[data.item] then
        Bought:send(player, { item = data.item })
    end
end)
--!client
local Buy = net.message("buy", { item = "string" })
local Bought = net.message("bought", { item = "string" })

local status = ui.label { text = "Pick something", x = 20, y = 20, size = 24 }
local sword = ui.button { text = "Buy a sword", x = 20, y = 60, width = 160 }

sword.clicked:connect(function()
    Buy:send { item = "sword" }
end)
Bought:listen(function(data)
    status.text = "You bought a " .. data.item
end)

A key press

--!client
input.key_down:connect(function(key)
    if key == "e" then
        print("E pressed with the cursor at", input.cursor)
    end
end)