Experimental

An early preview: docs, the API and the dashboard may change or break.

Report a problem

Scripting basics

Your scripts run on the server. Every server gets its own copy of them, starting with the world's entry script, main.luau.

Scripts start with only three globals: print, task and require. Everything else comes from a module.

Modules

Load a module with require and store it in a local:

--!strict
local players = require("@ember/players")
local world = require("@ember/world")
local types = require("@ember/types")

type Player = types.Player
ModuleUse it for
@ember/playersThe players on this server, and knowing when they join or leave.
@ember/worldBlocks, the map's markers and regions, and players placing or breaking blocks.
@ember/instanceWhich world and map this server is running, its [config], and shutting down.
@ember/uiTitles, the action bar, and UI that updates itself, like the sidebar.
@ember/dataSaving data for each player, and leaderboards.
@ember/itemsMaking item stacks to give to players or compare against.
@ember/signalCreating your own signals.
@ember/typesType names, like types.Player. There are no global types.
@std/roundA ready-made waiting room, countdown, timed round and results screen.
@pack/minecraftVanilla blocks and items, if your experience uses the Minecraft pack.
@contentYour experience's own blocks and items.

Glowstone Studio type-checks every file in strict mode. The templates put --!strict at the top of each file so other editors do the same. Typos in module members or block and item names show up as errors before you ever run the game.

Your own modules

You can split your code into modules and require them by a path relative to the current file. From worlds/match/main.luau, require("./game/coins") loads worlds/match/game/coins.luau.

Every world can reach the whole experience folder. To use a module next to content.luau, go up two levels with ../../.

When main.luau runs

main.luau runs once, when the server starts, before any players have joined. Use the top of the file to set things up, and connect handlers to react to players later:

local players = require("@ember/players")
local instance = require("@ember/instance")

local COINS_TO_WIN: number = instance.config.coinsToWin

players.joined:connect(function(player)
	player:send(`<yellow>First to {COINS_TO_WIN} coins wins!`)
end)

instance.config holds the values from the [config] section of world.toml.

Signals

A signal tells you when something happens. You'll find them on the thing they're about, for example:

signal:connect(handler) runs the handler every time the signal fires. It returns a connection; call connection:disconnect() to stop. signal:once(handler) runs it only the next time.

Handlers run on the tick after the event, so they're allowed to wait.

To make your own signal, call signal.new. It returns the signal and a function that fires it. Only code holding that function can fire the signal.

Intercepts

A signal tells you something has already happened. To stop something before it happens, use an intercept.

An intercept handler runs during the action. Return false to cancel it, or change the event's writable fields to alter what happens. Intercept handlers can't wait.

This stops players breaking anything except gold blocks, and stops gold blocks dropping items:

local world = require("@ember/world")
local minecraft = require("@pack/minecraft")

local GOLD = minecraft.blocks.gold_block.id

world.breaking:intercept(function(event)
	if event.block.id ~= GOLD then
		return false
	end
	event.drops = false
	return true
end)

Block and item ids

Scripts refer to blocks and items by id:

  • A plain id like "coin" means one of your own.
  • An id like "minecraft:gold_block" only works if experience.toml lists "minecraft" in uses. Otherwise it's an error.

Instead of typing ids by hand, get them from a module. The type checker knows these, so a typo is caught straight away:

local minecraft = require("@pack/minecraft")
local content = require("@content")

local GOLD = minecraft.blocks.gold_block.id
local COIN = content.items.coin.id

Time

All times are in seconds. The game runs at 20 ticks per second.

  • task.delay(seconds, fn) runs a function later.
  • task.wait(seconds) pauses the current code. It works inside signal handlers and code started with task.spawn, task.defer or task.delay, but not inside intercepts.
  • task.now() is the game's clock. os.time() is the real-world time.
task.delay(instance.config.roundSeconds, function()
	print("Time's up")
end)

Rounds and ending the game

A server shuts down once its last player leaves. Just before, instance.closing fires, giving you a few seconds to save anything. To send a player back to the lobby yourself, call player:travelHome().

Most games have a countdown, a timed round and a results screen. You don't have to build these yourself: @std/round does it for you.

local instance = require("@ember/instance")
local round = require("@std/round")

local game = round.start({
	minPlayers = 1,
	countdown = 5,
	duration = instance.config.roundSeconds,
	title = "<yellow>Coin Rush",
})

game.timedOut:connect(function()
	game:finish({ winners = leaders(), reason = "Time's up" })
end)

game:finish shows the results and sends everyone home. Here, leaders() is your own function that returns the winning players.

Saving data and live UI

Live UI. Store state in a ui.value. Any UI that reads it, like a ui.sidebar, redraws itself when the value changes.

Saved data. data.player sets up a saved document for each player, from the defaults you give it. Call it at the top of main.luau; it can't be called later.

local players = require("@ember/players")
local data = require("@ember/data")

local profile = data.player({ visits = 0, wins = 0 })

players.joined:connect(function(player)
	profile:get(player).visits += 1
end)

Leaderboards. data.leaderboard keeps each player's best score across every server.

Rules and limits

  • Text uses MiniMessage tags ("<red>hi</red>") and can be up to 4096 characters.
  • Once a player leaves, using their Player raises an error. Check player.alive first.
  • A Player's fields are read-only. Change things with its methods, like player:setHealth(20).
  • Positions are Luau vectors: vector.create(x, y, z).
  • If a handler or task errors, the error appears in the creator console and the rest of your script keeps running. More than 100 errors in one minute stops the experience.
  • Up to 2048 tasks can be waiting at once, and a world can change up to 4096 blocks per tick. Going over raises an error straight away, so you'll know.

Use print to write to the creator console while you work.