Scripting overview

Studz games are scripted in Dialect: Luau (a fast, typed relative of Lua) with an API designed for Studz rather than copied from anywhere. This page covers what you need before your first script: the language, where a script runs, the rules every script lives by, and what is not here yet.

The language

Scripts are Luau. If you know Lua you know most of it, and the parts Lua lacks are the parts you will want:

local total = 0
for i = 1, 10 do
    if i == 5 then
        continue          -- skip an iteration
    end
    total += i            -- compound assignment
end
print(`total is {total}`) -- string interpolation
  • Type annotations are part of the syntax (local speed: number = 16). They are accepted and ignored when the script runs.
  • Vectors are a built-in type that costs nothing to make: vec(1, 2, 3), with +, -, *, / and methods such as :dot, :cross, :length, :unit and :lerp.
  • Numbers are all doubles. There is no separate integer type.
  • There is no goto. Libraries are loaded with require (see Components, behaviors and modules).

Where a script runs

A script is a file. There are no Script, LocalScript and ModuleScript classes: a directive on the first lines says where it runs.

DirectiveMeaning
--!serverRuns on the game server, the trusted side. This is the default.
--!clientRuns on each player's machine.
--!sharedRuns on both.
--!moduleA library other scripts will load. It does not run by itself.
--!strict, --!nonstrict, --!nocheckHow strictly the type checker treats the script.

Directives are read from the top of the file, before any code. Blank lines and ordinary comments may come between them.

--!server
--!strict
-- This script runs on the game server and is type checked strictly.
print("hello from the server")

A mistake in a directive is an error, not a guess, because a typo that quietly changes where code runs would be dangerous:

--!sever
-- line 1: unknown directive `--!sever` (did you mean `--!server`?)

A script cannot be both server and client: write --!shared for both.

Player downloads omit server and Studio script source. An explicit directive takes priority over the script class saved by an older editor. Content under ServerStorage or ServerScriptService stays on the server, including nested descendants.

Your first script

Put this in a script in Polycarbonate and press Play:

--!server
world.joined:connect(function(player)
    player:tell("Welcome, " .. player.name .. "!")
end)

world is the world. joined is a signal: a value you connect functions to. See Signals, scopes and time for how signals and tasks work, and The world for entities and queries.

Errors that help

Dialect turns many silent mistakes into errors that say what to fix.

Unknown members are errors

part.transparancy = 1
-- Part has no member 'transparancy' (did you mean 'transparency'?)

Wrong values are errors

part.transparency = 2
-- Part.transparency must be between 0 and 1 (got 2)

part.position = "here"
-- Part.position expects a vector (vec(x, y, z)), got a string

Numbers and vectors must be finite, so NaN and infinity are refused and one bad calculation cannot corrupt a position.

A part is never half made

Part.new { name = "Door", transparency = 5 }
-- Part.transparency must be between 0 and 1 (got 5)
-- (nothing was created)

Every property is checked before the entity exists.

Other checks

You writeWhat happens
Assign to a read-only property (sound.playing = true)Error.
Assign to a signal (part.touched = 1)Error: connect to it instead.
part.parent = partError: an entity cannot be its own ancestor.
Read a property of an entity that was destroyedError: it was destroyed. Test entity.alive first.
Call an engine method with a wrong or missing argumentError naming the argument; nothing is queued.
sleep outside a taskError: start one with scope:spawn(fn).

An error in one task or handler is reported in the Output with the script's name and ends only that task. Everything else carries on.

Limits

Game servers share a machine, so everything a script can use has a ceiling. Hitting one raises an error in the script that did it. Nothing else is affected.

WhatLimit
TimeA task, handler or step may run for 0.1 seconds at a time. Nested handlers share the outer slice. Past it the script is stopped with script exceeded its time limit, and no pcall can catch it: once the time is up, everything the script runs raises the error again. A task that waits (sleep, signal:wait()) is not running while it waits.
Time per tickAll tasks together may run for 1 second between two steps of the game. Past it no further task is started until the next step, and the one that was refused is stopped with an error. Deferred functions (scope:defer) are tasks like any other: they have the 0.1 second limit too, even when the engine runs them because an object was destroyed.
Memory64 MB per game server for all scripts together. Past it an allocation fails with a memory error in the script that asked.
ObjectsAt most 250,000 objects in the world: Part.new fails past it. At most 50,000 engine calls (part:push and the like) may be waiting to be carried out at once; past it the call raises an error.
Script textScripts are source text. Compiled Luau code is never loaded: a script holding control characters (other than tab and line ends) is refused, when it is run, required, checked in the editor or published.
OutputThe last 2,000 lines are kept. A single line is cut at 2,048 characters.
AnnouncementsAt most 256 characters.

Sandbox

Scripts cannot reach the machine they run on. There is no io, no os.execute, no debug, no loadstring, and the standard library tables are read-only. require loads only the --!module scripts of your own game, by name; it never reads files. Each script has its own globals: one script's leaked = 1 is invisible to another.

What is not here yet

Dialect is new, and some features are not available in scripts yet:

  • The camera and the character from client scripts. A client script can read the world and draw on the screen, but cannot move the camera or the player's character: those are the game's own.
  • Behaviors in the property panel. Behaviors are attached from scripts; choosing them and their parameters in Polycarbonate is planned.
  • Tools, seats, vehicles and NPC characters. The game no longer has them built in. They can be written as Dialect packages; none exists yet.

Where to go next

If you want to...Read
React to touches, clicks and joins; wait; repeatSignals, scopes and time
Make, find and change things; ask the world a questionThe world
Work with players, sound and chatPlayers and the game
Read keys, draw on the screen, talk to the serverClient scripts and interface
Reuse behavior across parts, share libraries, animate propertiesComponents, behaviors and modules
Remember things between visitsSaved data
Look up a property or methodAPI reference
Bring scripts over from the earlier APIMoving from the earlier API
Copy a working scriptExamples