Skinstashery
Roblox
Home / Roblox / Studio script library / Round timer
RB-004

Roblox round timer script

A loop of waiting, intermission and round, with the time left shared in a StringValue.

Script type
Script
Where it goes
ServerScriptService
Category
Game flow

For your own game in Roblox Studio. Not for use in other people's games.

The code

-- Round timer: waiting for players -> intermission -> round -> repeat.
-- The time left is written to ReplicatedStorage.RoundStatus (a StringValue).
-- Show it with a GUI that reads that value.

local MIN_PLAYERS = 2
local INTERMISSION_SECONDS = 15
local ROUND_SECONDS = 60
local RESPAWN_AT_ROUND_START = true   -- send everyone to the spawn when the round begins

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local status = ReplicatedStorage:FindFirstChild("RoundStatus")
if not status then
	status = Instance.new("StringValue")
	status.Name = "RoundStatus"
	status.Parent = ReplicatedStorage
end

local function enoughPlayers()
	return #Players:GetPlayers() >= MIN_PLAYERS
end

local function startRound()
	-- Put your own round code here: pick a map, give weapons, teleport players...
	if RESPAWN_AT_ROUND_START then
		for _, player in Players:GetPlayers() do
			task.spawn(function()
				player:LoadCharacter()
			end)
		end
	end
end

local function endRound()
	-- Put your own end of round code here: show a winner, give rewards...
end

while true do
	-- 1. Wait until enough players are in.
	while not enoughPlayers() do
		status.Value = "Waiting for players (need " .. MIN_PLAYERS .. ")"
		task.wait(1)
	end

	-- 2. Intermission.
	for secondsLeft = INTERMISSION_SECONDS, 1, -1 do
		status.Value = "Next round in " .. secondsLeft
		task.wait(1)
	end

	-- 3. The round.
	if enoughPlayers() then
		startRound()
		for secondsLeft = ROUND_SECONDS, 1, -1 do
			if not enoughPlayers() then
				break
			end
			status.Value = "Round: " .. secondsLeft
			task.wait(1)
		end
		endRound()
	end
end

How to set it up

  1. Add the Script to ServerScriptService.
  2. To test alone, set MIN_PLAYERS to 1.
  3. Play. Select ReplicatedStorage > RoundStatus in the Explorer and watch the Value change.
  4. To show it on screen, add a TextLabel and a LocalScript that sets the text to RoundStatus.Value whenever RoundStatus.Changed fires.

Change it

More in Game flow

All Roblox scripts

More Roblox tools