Skip to main content

Round System

This example shows a simple round system using Reheaven.

The round has a status, time left, and whether players can join.

Key Point

Round state controls what the game is currently doing.

Round State​

RoundState.lua
local signal = Reheaven.signal

local roundStatus, setRoundStatus = signal("Intermission")
local timeLeft, setTimeLeft = signal(30)
local playersCanJoin, setPlayersCanJoin = signal(true)

local roundActive = Reheaven.computed(function()
return roundStatus() == "InRound"
end)

Round Actions​

RoundActions.lua
local batch = Reheaven.batch

local function startIntermission()
batch(function()
setRoundStatus("Intermission")
setTimeLeft(30)
setPlayersCanJoin(true)
end)
end

local function startRound()
batch(function()
setRoundStatus("InRound")
setTimeLeft(120)
setPlayersCanJoin(false)
end)
end

local function endRound()
batch(function()
setRoundStatus("Ended")
setTimeLeft(0)
setPlayersCanJoin(false)
end)
end

Timer Loop​

RoundTimer.lua
task.spawn(function()
while true do
task.wait(1)

if timeLeft() > 0 then
setTimeLeft(timeLeft() - 1)
elseif roundStatus() == "Intermission" then
startRound()
elseif roundStatus() == "InRound" then
endRound()
end
end
end)

Round UI​

RoundUI.lua
local effect = Reheaven.effect

effect(function()
statusLabel.Text = roundStatus()
timerLabel.Text = `{timeLeft()}s`
end)

effect(function()
joinButton.Visible = playersCanJoin()
end)

Round Flow​

Intermission
↓
Round starts
↓
Timer counts down
↓
Round ends
↓
Intermission starts again
Keep the Server in Control

Round state should usually be controlled by the server.

Clients can display the round state, but the server should decide when rounds start, end, and reset.