How to end round when everyone is dead

I’m making a game with different rounds, and when everyone dies, I want the round to end and to send everyone back to the lobby. I don’t know how to do this however, because if I wanted to check each time someone dies, there’s no easy way to signal to the script that manages the rounds, but if I checked inside the script that manages the rounds, I would have to check every second, and I don’t know if this is very efficient or not. How should I do this?

You could have 2 folders in workspace, one named “Alive” and the other “Dead”

When someone dies, put them in the “Dead” folder, and then just do a repeat check until theres nobody in the “Alive” folder and then end the round

local Players = game:GetService("Players")

local players = Players:GetPlayers()
local playersLeft = #players

local function roundEnded()
	
end

for _, player in players do
	player.Character.Humanoid.Died:Connect(function()
		playersLeft -= 1
		
		if playersLeft == 0 then
			roundEnded()
		end
	end)
end

sample code

First of all, you can create a table to manage all the players. Once the round starts add every player to the table.

Here is a sample code to remove the players from the table:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)	
		char.Humanoid.Died:Connect(function()
			for i, v in pairs(playerTable) do			
				if v == player then
					table.remove(playerTable, i)
				end
			end
		end)
	end)
end)

Every time a player dies you can check how many players are left in the table. So for your case, once there are 0 players left you can create a function that runs called “roundEnded” and run it

@bytesleuth @AdSomnum @tnt_Dante
those solutions wouldn’t work because for the round to end, there has to be a break in the loop:

while true do
	--intermission
	timeUntil.Value = "Intermission"
	for seconds = 20, 0, -1 do
		timeCounter.Value = seconds
		task.wait(1)
	end
	if #Players:GetPlayers() <= 1 then
		while #Players:GetPlayers() <= 1 do
			timeUntil.Value = "Not enough players."
			task.wait(3)
			timeUntil.Value = "Intermission"
			for seconds = 20, 0, -1 do
				timeCounter.Value = seconds
				task.wait(1)
			end
		end
	end
	--set gravel
	local gravelPlayer = Players:GetPlayers()[math.random(1, #Players:GetPlayers())]
	gravel:FireClient(gravelPlayer)
	gravelPlayer.Team = gravelTeam
	print(gravelPlayer.Name .." is now gravel")
	local gravelChar = gravelPlayer.Character
	if not gravelChar or not gravelChar.Parent then
		gravelPlayer.CharacterAdded:Wait()
	end
	local gravelHrp = gravelChar:WaitForChild("HumanoidRootPart")
	local gravelHumanoid = gravelChar:WaitForChild("Humanoid")
	gravelHrp.CFrame = gravelWait.CFrame
	gravelHumanoid.WalkSpeed = 20
	-- send players to game
	for i, plr in pairs(lobbyTeam:GetPlayers()) do
		runners:FireClient(plr)
		plr.Team = runnersTeam
		local char = plr.Character
		if not char or not char.Parent then
			char.CharacterAdded:Wait()
		end
		local hrp = char:WaitForChild("HumanoidRootPart")
		hrp.CFrame = runnerSpawn.CFrame
	end
	-- headstart
	timeUntil.Value = "Gravel spawns in:"
	for seconds = 15, 0, -1 do
		timeCounter.Value = seconds
		task.wait(1)
	end
	-- start game(this loop is where the break has to be)
	gravelHrp.CFrame = gravelSpawn.CFrame
	timeUntil.Value = "Game in progress."
	for seconds = 300, 0, -1 do
		timeCounter.Value = seconds
		task.wait(1)
	end
	-- game over/send to lobby
	timeUntil.Value = "Game Over"
	gravelHumanoid.WalkSpeed = 16
	lobby:FireClient(gravelPlayer)
	for i, plr in pairs(Players:GetPlayers()) do
		plr.Team = lobbyTeam
		local char = plr.Character
		if not char or not char.Parent then
			char.CharacterAdded:Wait()
		end
		local hrp = char:WaitForChild("HumanoidRootPart")
		hrp.CFrame = lobbySpawn.CFrame
	end
	task.wait(3)
end

Is there a more efficient way than checking how many players are left each second?

Nevermind, I just checked every second if any players were in the “runners” team and it worked.

If you want to break a loop in the roundEnded function then feel free too

What I like to do is add a StringValue into every player’s character, then during the round loop, check which players still have the StringValue inside of their character. If they don’t, then they died.

I keep a table during the round of all players that are still in the round in the check if they have it. I clear the table every time it comes to the check, insert every player who has the StringValue into the table, then I have another check for conditions to end the game. To check if all of the player’s are dead, I check if the number of items in the table, and if it is zero, I call break to end the round.

I keep all of that inside of the time for loop so it checks every second.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.