Menu
CoddyTech

Who Is Alive

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

To know who won, the game must know who is still alive. A table keyed by player works as a set: survivors[player] = true adds a player, survivors[player] = nil removes one, and for player in survivors do visits whoever is left.

A Humanoid fires Died when its health reaches 0. Connecting to it when the round starts takes a player out of the set the moment they fall:

survivors[player] = true
character.Humanoid.Died:Connect(function()
	survivors[player] = nil
end)

Only players alive when the round starts join it: someone waiting to respawn has a dead character with Health 0, and teleporting it would count a dead player as a survivor.

challenge icon

Challenge

Medium

Keep track of who is still alive in the round.

  1. When the round starts, make local survivors = {}.
  2. Teleport only players whose character has Humanoid.Health above 0, and add each to survivors.
  3. Connect each one's Humanoid.Died to survivors[player] = nil.
  4. When the round ends, teleport only the survivors back, then print "Survivors: " .. count.

Press Play: when the round ends the Output reads Survivors: 1. 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

	for _, player in Players:GetPlayers() do
		-- Lesson 7: Who Is Alive goes here
		teleport(player, map.Spawn)
	end

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

	for _, player in Players:GetPlayers() do
		teleport(player, lobbySpawn)
	end
	map:Destroy()
end

All lessons in Roblox Game: Disaster Survival

Practice on your own: Roblox playground