task
Coroutine scheduling, 20 ticks a second. At most 2048 tasks may be pending; scheduling past that raises.
Functions
Runs fn as a task soon after the current code finishes, within a tick.
local world = require("@ember/world")
local players = require("@ember/players")
-- `defer` runs a function once the code already queued for this tick has finished. Here a burst of
-- breaks in one tick, from several players at once, sends one message instead of one per block.
local broken = 0
local announcing = false
local function announce()
announcing = false
for _, player in players.all() do
player:send(`<gold>{broken} blocks broken so far.`)
end
end
world.broken:connect(function()
broken += 1
if not announcing then
announcing = true
task.defer(announce)
end
end)Runs fn as a task after seconds, rounded up to whole ticks.
local players = require("@ember/players")
local items = require("@ember/items")
-- Run something later without pausing anything: the kit arrives 10 seconds after joining.
players.joined:connect(function(player)
player:send("<aqua>Your kit arrives in 10 seconds. Find cover!")
task.delay(10, function()
-- They may have left in the meantime.
if player.alive then
player.inventory:add(items.create("minecraft:iron_sword"))
player.inventory:add("minecraft:golden_apple", 2)
end
end)
end)Wall time in seconds since the Unix epoch, to the millisecond. It keeps running while the world is frozen or lagging, and between sessions, so a timestamp saved in a document stays valid.
local data = require("@ember/data")
local players = require("@ember/players")
-- A daily reward. `task.epoch` is real-world time, so it keeps counting between sessions and a
-- saved timestamp stays meaningful; `task.now` restarts with every instance.
local profile = data.player({ lastReward = 0 })
local DAY = 24 * 60 * 60
players.joined:connect(function(player)
local saved = profile:get(player)
local now = task.epoch()
if now - saved.lastReward >= DAY then
saved.lastReward = now
player.inventory:add("coin", 100)
player:send("<gold>Daily reward: 100 coins!")
else
local hours = math.ceil((saved.lastReward + DAY - now) / 3600)
player:send(`<gray>Next daily reward in {hours} hours.`)
end
end)Game time in seconds: ticks the instance has run, over 20. Stops while the world is frozen. Use os.time() and os.date("!*t") for real-world dates.
local world = require("@ember/world")
-- Game time in seconds. Use it to time things that happen in the world, like a race lap.
local lapStart: { [string]: number } = {}
world.regions.single("finish_line").entered:connect(function(player)
local now = task.now()
local started = lapStart[player.id]
if started then
player:send(string.format("<green>Lap: %.1fs", now - started))
end
lapStart[player.id] = now
end)Runs fn with the arguments now, as a task that may task.wait; given a suspended thread, resumes it with the arguments instead.
local players = require("@ember/players")
local types = require("@ember/types")
-- Each spawned task runs on its own, so every player gets their own countdown.
local function countdown(player: types.Player, seconds: number)
for left = seconds, 1, -1 do
player:send(`<yellow>{left}...`)
task.wait(1)
end
player:send("<green>Go!")
end
players.joined:connect(function(player)
task.spawn(countdown, player, 3)
end)Pauses the current task for seconds (at least one tick) and returns the seconds actually waited. Only works inside a task (spawn, defer, delay, or a signal handler); raises elsewhere, such as in an intercept.
local players = require("@ember/players")
local world = require("@ember/world")
-- There is no per-tick event: to run code every tick, loop in a task. `task.wait()` with no
-- argument waits one tick, a twentieth of a second, and returns the seconds actually waited.
task.spawn(function()
while true do
task.wait()
for _, player in players.all() do
-- Anyone who falls off the map is back at spawn the same tick.
if player.position.y < -16 then
player:teleport(world.spawn, world.spawnLook)
end
end
end
end)
-- `task.wait(seconds)` waits that long, rounded up to whole ticks. It pauses only the task it runs
-- in, so each loop gets its own `task.spawn`.
task.spawn(function()
while true do
task.wait(60)
for _, player in players.all() do
player:send("<gray>Tip: break gold blocks for coins.")
end
end
end)