Scripts that run for the whole place¶
An autorun script is a script file in the project's scripts/autorun/ folder,
or a folder below it, that runs for the whole place instead of on a part.
Round managers and heads-up displays are autorun scripts. Its extension
decides where it runs:
.server.luauruns once on the server when the place starts..client.luauruns once on each player after the place has loaded, and keeps running across respawns..luauis a module and runs only when a script loads it withrequire.
Autorun scripts start in sorted path order, with self nil. A global
Update or PhysicsUpdate
runs as it does on a part, but only
when the script declares it before its first top-level wait, such as
task.wait or WaitFor. Put waits inside task.spawn instead.
A file that Studio or another program writes into the folder joins the project and runs in the next playtest.
Examples¶
-- Server script scripts/autorun/Spinner.server.luau: turn every part tagged Spinner.
local DEGREES_PER_SECOND = 90.0
local Spinner = World.DefineComponent("Spinner")
local spinners = World.Query(Spinner)
function Update(dt: number)
for part in spinners do
local current = part.Rotation
if current then
part.Rotation = current * Rotation.fromEuler(Vector3.new(0, DEGREES_PER_SECOND * dt, 0))
end
end
end
-- Server script scripts/autorun/Coins.server.luau: a coin tagged Coin gives one coin.
local CoinChanged = Net.ToClients("CoinChanged", {
total = 0,
})
local coins: { [number]: number } = {}
Component.User.Added:Connect(function(player: Entity)
local user = player.User
if user then
coins[user.Id] = 0
end
end)
Component.User.Removed:Connect(function(player: Entity)
local user = player.User
if user then
coins[user.Id] = nil
end
end)
local Coin = World.DefineComponent("Coin")
for coin in World.Query(Coin) do
coin:On(Component.Collider.Touched, function(hitPart: Entity)
local player = Players.Of(hitPart)
local user = player and player.User
if player and user then
local total = (coins[user.Id] or 0) + 1
coins[user.Id] = total
Net.SendTo(player, CoinChanged, { total = total })
coin:Destroy()
end
end)
end
-- Client script scripts/autorun/HUD.client.luau: show the coin count.
local CoinChanged = Net.ToClients("CoinChanged", {
total = 0,
})
local hud = World.Spawn({ UIRoot = true })
hud.Name = "HUD"
hud.Parent = World.Viewport.Screen
local label = World.Spawn({
Box = {
Size = UIVector.fromPoints(200, 40),
Position = UIVector.fromPoints(20, 20),
},
UITextSource = { Text = "Coins: 0" },
})
label.Name = "CoinsLabel"
label.Parent = hud
Net.On(CoinChanged, function(payload: { total: number })
local view = label.UITextSource
if view then
view.Text = "Coins: " .. tostring(payload.total)
end
end)
To test server autorun scripts without Studio, run
luduvo-run <project-dir> --ticks 1200 from the Luduvo install folder. It
runs the place headless for that many ticks, 60 a second, prints script
output, and exits with status 1 when a script reports an error. Add
--players 2 to open two player windows, whose output starts with
[Player1] or [Player2].