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
endAnything 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
EasyTurn the countdown into the game's loop:
- Wrap it in
while true do ... end. - Replace
Get ready!with the round: count down fromSettings.RoundLengthto 1, a second apart, showingTime left: 15,Time left: 14and 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!")
This lesson includes a short quiz. Start the lesson to answer it and track your progress.