Menu
CoddyTech

Winners

Lesson 8 of 20 in Coddy's Roblox Game: Disaster Survival course.

Surviving a round is worth a Win. When the round ends, every player still in survivors gets one:

for player in survivors do
	player.leaderstats.Wins.Value += 1
end

+= adds to a value in place. To tell whether anyone survived, next(survivors) gives the table's first key, or nil when it is empty.

Players need a moment to read the result before the lobby countdown takes the board back, so the round ends with task.wait(3).

challenge icon

Challenge

Medium

Give every survivor a Win.

  1. When the round ends, add 1 to each survivor's Wins instead of counting them.
  2. After the map is destroyed, show Survivors win! if anyone survived, else Nobody survived!
  3. Then task.wait(3) before the next lobby countdown.

Press Play and survive a round: Wins goes up to 1 and the board says Survivors win. Then Check.

Try it yourself

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

local lobbySpawn = workspace.LobbySpawn

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

local function teleport(player, target)
	local character = player.Character
	if character then
		character:PivotTo(target.CFrame + Vector3.new(0, 4, 0))
	end
end

-- The round loop
while true do
	for seconds = Settings.Intermission, 1, -1 do
		setStatus("Intermission: " .. seconds)
		task.wait(1)
	end

	local map = ServerStorage.Maps.Island:Clone()
	map.Name = "Map"
	map.Parent = workspace

	local survivors = {}
	for _, player in Players:GetPlayers() do
		local character = player.Character
		if character and character.Humanoid.Health > 0 then
			teleport(player, map.Spawn)
			survivors[player] = true
			character.Humanoid.Died:Connect(function()
				survivors[player] = nil
			end)
		end
	end

	for seconds = Settings.RoundLength, 1, -1 do
		setStatus("Time left: " .. seconds)
		task.wait(1)
	end

	local count = 0
	for player in survivors do
		count += 1
		teleport(player, lobbySpawn)
	end
	map:Destroy()
	-- Lesson 8: Winners goes here
	print("Survivors: " .. count)
end

All lessons in Roblox Game: Disaster Survival

Practice on your own: Roblox playground