The Round Loop
Lesson 4 of 20 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 a game loop that never ends.
- Wrap the countdown in
while true do ... end. - Replace
Get ready!with the round: count down fromSettings.RoundLengthto 1, a second apart, showing"Time left: " .. seconds.
Press Play: Time left counts down from 15, then the lobby countdown starts 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
-- Lesson 4: The Round Loop goes here
-- The countdown
for seconds = Settings.Intermission, 1, -1 do
setStatus("Intermission: " .. seconds)
task.wait(1)
end
setStatus("Get ready!")
All lessons in Roblox Game: Disaster Survival
Practice on your own: Roblox playground