Menu
CoddyTech

The Round Loop

Lesson 4 of 18 in Coddy's Roblox Game: Disaster Survival course.

A round game never ends: lobby, round, lobby, round. That is a while loop whose condition is always true:

while true do
	-- the lobby countdown
	-- the round
end

Anything inside runs again from the top each time it reaches end. Because a while true loop never finishes, it goes at the very bottom of the script: code after it would never run. Every pass must also wait somewhere, or the loop would never give the rest of the game a turn and Roblox would stop the script.

The round counts down too, from Settings.RoundLength, so players see how long they have to survive.

challenge icon

Challenge

Easy

Turn the countdown into the game's loop:

  1. Wrap it in while true do ... end.
  2. Replace Get ready! with the round: count down from Settings.RoundLength to 1, a second apart, showing Time left: 15, Time left: 14 and so on.

Press Play and watch a round go by and the lobby countdown start again, then Check.

Try it yourself

local Players = game:GetService("Players")
local Settings = require(game.ReplicatedStorage.Settings)

print("Rounds last " .. Settings.RoundLength .. " seconds")

-- Every player gets Wins on the leaderboard
Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Parent = leaderstats
end)

-- The status board in the lobby
local gui = Instance.new("SurfaceGui")
gui.Parent = workspace.StatusBoard

local status = Instance.new("TextLabel")
status.Name = "Status"
status.Size = UDim2.fromScale(1, 1)
status.BackgroundColor3 = Color3.fromRGB(25, 25, 35)
status.TextColor3 = Color3.fromRGB(255, 255, 255)
status.TextScaled = true
status.Text = "Welcome to Disaster Survival!"
status.Parent = gui

local function setStatus(text)
	status.Text = text
end

-- The countdown
for seconds = Settings.Intermission, 1, -1 do
	setStatus("Intermission: " .. seconds)
	task.wait(1)
end
setStatus("Get ready!")
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Roblox Game: Disaster Survival