@ember/world
This instance's world: its blocks, the map's markers and regions, and what players do to blocks.
local world = require("@ember/world")Signals
world.breaking: types.Intercept<types.BreakEvent>Runs before a player breaks a block; return false to stop it, or set drops to keep its loot.
local world = require("@ember/world")
-- Only wool can be broken, and it never drops. Runs before the break happens and must not yield.
world.breaking:intercept(function(event)
if not string.find(event.block.id, "_wool$") then
event.player:send("<red>You can only break wool.")
return false
end
event.drops = false
return true
end)world.broken: types.Signal<types.BreakEvent>Fires after a player breaks a block nothing stopped.
local world = require("@ember/world")
local ui = require("@ember/ui")
-- Runs after a break nothing stopped: count it and put the block back a few seconds later.
local broken = 0
world.broken:connect(function(event)
broken += 1
ui.actionBar(event.player, `<gold>Blocks broken: {broken}`)
local position, id, state = event.position, event.block.id, event.block.state
task.delay(5, function()
world.setBlock(position, id, state)
end)
end)world.placed: types.Signal<types.PlaceEvent>Fires after a player places a block nothing stopped.
local world = require("@ember/world")
-- Fires after a block is placed, if nothing refused it. Remember what players built so it can be
-- cleared at the end of the round.
local built: { vector } = {}
world.placed:connect(function(event)
table.insert(built, event.position)
end)
local function clearBuilds()
for _, position in built do
world.setBlock(position, "minecraft:air")
end
table.clear(built)
end
task.delay(600, clearBuilds)world.placing: types.Intercept<types.PlaceEvent>Runs before a player places a block; return false to stop it.
local world = require("@ember/world")
-- Keep the spawn area clear: refuse any block placed within 8 blocks of it.
world.placing:intercept(function(event)
if vector.magnitude(event.position - world.spawn) < 8 then
event.player:send("<red>No building at spawn.")
return false
end
return true
end)Fields
Functions
world.blockData(position: vector) -> { [string]: any }?The data of a custom block whose type declares a data schema, or nil. Setting another block there, or breaking it, drops the data. At most 4 KB of JSON per block.
local blocks = require("@ember/blocks")
local world = require("@ember/world")
-- A custom block can carry its own data. In content.luau:
--
-- content.block("mailbox", { visual = { pool = "note_block" }, data = { owner = "", letters = 0 } })
--
-- `blockData` hands back the block's own table, so changes to it stay.
local mailbox = blocks.type("mailbox")
mailbox.placed:connect(function(event)
local box = world.blockData(event.position)
if box then
box.owner = event.player.id
end
end)
mailbox.used:connect(function(event)
local box = world.blockData(event.position)
if not box then
return
end
if box.owner == event.player.id then
event.player:send(`<yellow>You have {box.letters} letters.`)
else
box.letters += 1
event.player:send("<green>Letter delivered.")
end
end)world.cancelTick(position: vector)Cancels the tick scheduleTick set at the position, if any.
Reads the blocks of a box into a structure held by this script, in the frame at origin facing facing (default the box's min corner, north). Yields while chunks load. At most 2,000,000 blocks.
Returns the block at a whole-number position. Raises where the world is not loaded.
local world = require("@ember/world")
local players = require("@ember/players")
-- Every second, anyone standing on lava-red terracotta takes a hit.
task.spawn(function()
while true do
task.wait(1)
for _, player in players.all() do
local below = vector.floor(player.position) - vector.create(0, 1, 0)
if world.getBlock(below).id == "minecraft:red_terracotta" then
player:setHealth(math.max(player.health - 4, 0))
end
end
end
end)Returns every marker with the name, in map order.
local world = require("@ember/world")
local players = require("@ember/players")
-- Markers are named points placed in the map editor. Spread players over the "spawn" markers.
local spawns = world.markers.get("spawn")
local turn = 1
players.joined:connect(function(player)
local marker = spawns[turn]
turn = turn % #spawns + 1
-- A marker's yaw, if it has one, becomes the way the player faces.
player:teleport(marker.position, { yaw = marker.yaw or 0, pitch = 0 })
end)Returns the one marker with the name. Raises unless the map has exactly one.
Writes a structure's blocks in the frame at at facing facing (default the structure's own), turning each block state by the quarter turns between the two. Yields until every cell is written, spending this script's 4096 block writes a tick; cells already holding their block are skipped and not counted. Loads the chunks it writes; raises before writing anything when a cell is outside the map. No block signals fire.
local world = require("@ember/world")
-- Reset the arena between rounds: copy it once at startup, paste it back after each round. Both
-- spread their work over ticks and yield, so call them from a task.
-- "arena" is a box region drawn in the map editor.
local arena = world.regions.single("arena").shape
local origin = vector.floor(world.markers.single("arena_origin").position)
task.spawn(function()
local snapshot = world.capture(arena, { origin = origin })
while true do
task.wait(300)
-- Only the blocks that changed are written.
world.paste(snapshot, origin)
end
end)Creates a region. Its occupants are counted from the next tick.
local world = require("@ember/world")
local ui = require("@ember/ui")
-- A capture zone made from a script: a cylinder 6 blocks wide around the "flag" marker.
local flag = world.markers.single("flag").position
local zone = world.regions.create("capture", { kind = "cylinder", base = flag, radius = 3, height = 4 })
zone.entered:connect(function(player)
ui.actionBar(player, "<yellow>Capturing...", 2)
end)
-- `players` is who's inside as of this tick.
task.spawn(function()
while zone.alive do
task.wait(1)
if #zone.players == 1 then
zone.players[1]:send("<green>+1 point")
end
end
end)Returns the first region with the name, or nil.
local world = require("@ember/world")
-- A "void" region drawn under the map catches anyone who falls off and puts them back.
local void = world.regions.first("void")
if void then
void.entered:connect(function(player)
player:teleport(world.spawn, world.spawnLook)
player:send("<gray>Careful near the edge!")
end)
endReturns every region with the name.
local data = require("@ember/data")
local world = require("@ember/world")
-- A parkour course with many checkpoints: one region named "checkpoint" each, numbered by an
-- `order` property set in the map editor.
local profile = data.player({ checkpoint = 0 })
for _, region in world.regions.get("checkpoint") do
local order = tonumber(region.properties.order) or 0
region.entered:connect(function(player)
local saved = profile:get(player)
if order > saved.checkpoint then
saved.checkpoint = order
player:setRespawn(player.position)
player:send(`<green>Checkpoint {order} reached!`)
end
end)
endReturns the one region with the name. Raises unless exactly one exists.
local ui = require("@ember/ui")
local world = require("@ember/world")
-- The map has exactly one region named "lava_pit"; `single` raises at startup if that changes, so
-- a broken map fails loudly. `left` also fires when a player disconnects while inside.
local pit = world.regions.single("lava_pit")
pit.entered:connect(function(player)
ui.actionBar(player, "<red>The floor is lava!", 2)
player:setHealth(math.max(player.health - 6, 0))
end)
pit.left:connect(function(player)
if player.alive then
ui.actionBar(player, "<green>Safe.", 1)
end
end)world.scheduleTick(position: vector, seconds: number)Fires the block type's ticked once after seconds, rounded up to whole ticks and at least one, if a block of that type still stands there. A position holds one tick: scheduling again replaces it, sooner or later.
Range: seconds: 0..86400
local world = require("@ember/world")
local blocks = require("@ember/blocks")
-- A "landmine" block declared in content.luau goes off three seconds after it's placed.
local landmine = blocks.type("landmine")
landmine.placed:connect(function(event)
event.player:send("<red>Armed! Three seconds...")
world.scheduleTick(event.position, 3)
end)
-- `ticked` fires only if a landmine still stands there.
landmine.ticked:connect(function(position)
world.setBlock(position, "minecraft:air")
end)world.setBlock(position: vector, id: string, state: { [string]: types.StateValue }?, data: { [string]: any }?)Places a fresh block at a whole-number position; state properties not named take their defaults. A block whose type declares a data schema takes data as its data: the table itself, not a copy, checked against the schema and filled from its defaults; without data it gets a fresh copy of the defaults. Raises outside the bounds or where the world is not loaded; each call is one of 4096 block writes a tick.
local world = require("@ember/world")
local types = require("@ember/types")
-- Build a 5x5 gold platform under the "arena" marker. Positions must be whole numbers.
local center = vector.floor(world.markers.single("arena").position)
for dx = -2, 2 do
for dz = -2, 2 do
world.setBlock(center + vector.create(dx, -1, dz), "minecraft:gold_block")
end
end
-- State properties not named take their defaults.
local stairs: { [string]: types.StateValue } = { facing = "north", half = "bottom" }
world.setBlock(center + vector.create(0, 0, 3), "minecraft:oak_stairs", stairs)world.setState(position: vector, changes: { [string]: types.StateValue })Changes only the named state properties of the block already there; its data stays. Raises for a property it doesn't have or a value out of range.
local world = require("@ember/world")
-- Change one property of a block in place, keeping the rest of its state: open the fence gate
-- while someone stands on the plate, and shut it when the last one steps off.
local gate = vector.floor(world.markers.single("vault_gate").position)
local plate = world.regions.single("vault_plate")
plate.entered:connect(function()
world.setState(gate, { open = true })
end)
plate.left:connect(function()
if #plate.players == 0 then
world.setState(gate, { open = false })
end
end)world.setTime(ticks: number, options: types.TimeOptions?)Sets the time of day in ticks; cycle = false holds it there until it is set again.
Range: ticks: 0..23999
local world = require("@ember/world")
-- Time is in ticks: 0 is sunrise, 6000 noon, 13000 nightfall and 18000 midnight. `cycle = false`
-- freezes the clock, the usual choice for a minigame map.
world.setTime(18000, { cycle = false })
-- A short day for a survival round: dawn after two minutes.
task.delay(120, function()
world.setTime(0)
end)world.setWeather(weather: types.Weather, options: types.WeatherOptions?)Sets the weather for everyone here, changing over transition seconds.
local players = require("@ember/players")
local world = require("@ember/world")
-- A storm rolls in over 10 seconds when the final phase starts.
task.delay(240, function()
world.setWeather("thunder", { transition = 10 })
for _, player in players.all() do
player:send("<dark_gray>A storm is coming...")
end
end)world.time() -> numberThe time of day in ticks, 0..23999: 6000 is noon and 18000 midnight.