How could I make it so that when all players die, the round ends?

Hey guys, I was wondering how I could make it so that when all players in the game die, the round ends? Any help is appreciated. Here is my code:

ServerStorage = game:GetService("ServerStorage")
ReplicatedStorage = game:GetService("ReplicatedStorage")
Players = game:GetService("Players")

Maps = ServerStorage:WaitForChild('Maps'):GetChildren()
Status = ReplicatedStorage:WaitForChild('Status')

while true do
	
	--Intermission
	
	local Countdown = 30 -- intermission, make this as long as you want
	
	repeat wait(1)
		Countdown = Countdown - 1
		
		Status.Value = 'Intermission : '..Countdown
	until Countdown <= 0
	
	--Choose the map.
	
	Status.Value = 'Choosing Map...'
	
	local ChosenMap = Maps[math.random(1, #Maps)]:Clone()
	local Spawns = ChosenMap:FindFirstChild('Spawns'):GetChildren()
	local RandomSpawn = Spawns[math.random(1, #Spawns)]
	
	wait(5) -- little pause, make this as long as you want
	
	ChosenMap.Parent = workspace
	Status.Value = 'Map chosen, teleporting players.'
	
	wait(2) -- little pause, make this as long as you want
	local Knife = ServerStorage.Knife
	--teleport the players
	
	for _, Player in pairs(Players:GetChildren())do
		if Player.Character and Player.Character:FindFirstChild('Humanoid') then
			Player.Character.HumanoidRootPart.CFrame = RandomSpawn.CFrame
local newKnife = Knife:Clone()
newKnife.Parent = Player.Backpack
		end
	end
	
	Countdown = 5 -- Starting Round In, make this as long as you want
	
	repeat wait(1)
		Countdown = Countdown - 1
		
		Status.Value = 'Starting Round in : '..Countdown
	until Countdown <= 0
	
	Countdown = 60 -- Game Time
	
	repeat wait(1)
		Countdown = Countdown - 1
		
		Status.Value = 'Ingame : '..Countdown
	until Countdown <= 0
	
	--Kill the players
	
	for _, Player in pairs(Players:GetChildren())do
		if Player.Character and Player.Character:FindFirstChild('Humanoid') then
			Player.Character.Humanoid:TakeDamage(2000)
		end
	end
	
	ChosenMap:Destroy()
	
	Status.Value = 'Round Ended, waiting for new game.'
	
	wait(4) -- little pause, make this as long as you want.
	
end

Thanks!

3 Likes

Now, everybody has a different way of approaching this, but here’s how I approach it.

  • Place a NumberValue in ReplicatedStorage and name it deathpoll.
  • Utilize this value into you’re script by gathering the players (recursion loop) and add a +1 to the deathpoll for each player that’s teleported into the round.
  • If a player dies, the deathpoll should go down. (Obviously that’s a separate script you’ll have to write within the Client. Because if the Client disconnects from the server, the deathpoll will be stuck at a certain number. Gotta be prepared for those unique scenarios.)

This NumberValue will take into account of how many players are in the game. When a player dies, the NumberValue goes down. If the value reaches 0, break the loop and end the round.

This is a really rough outline and I’m sure there’s flaws in it, so take it with a grain of salt.

1 Like

Create a table of the players that are in the round, and fill that table when you teleport the players. Listen for those player’s deaths and remove them from the table. Also listen for players removing to see if any players left mid round. Then you can just add another condition in your round timer to end the round if the list is empty. Remember to clean up the connections to prevent memory leaks.

This is the code I used to test this. Take the parts you need.
while true do
	-- intermission
	for i = 1, 5 do
		print("Intermission", i)
		wait(1)
	end
	
	-- add players to the table
	local playersInRound = {} -- track alive players
	local connections = {} -- store connections to disconnect later
	for index, player in pairs(game.Players:GetPlayers()) do
		table.insert(playersInRound, player) -- add all players to table

		connections[player.Name] = player.Character.Humanoid.Died:Connect(function() -- remove player from list on death
			table.remove(playersInRound, table.find(playersInRound, player))
		end)
	end
	
	connections["Removing"] = game.Players.PlayerRemoving:Connect(function(player) -- remove player from list if they leave
		local ind = table.find(playersInRound, player)
		if ind then
			table.remove(playersInRound, ind)
		end
	end)
	
	for i = 1, 15 do
		print("Playing round", i)
		if #playersInRound == 0 then
			break
		end
		wait(1)
	end
	
	for index, connection in pairs(connections) do -- disconnect connections to prevent memory leaks
		connection:Disconnect()
	end
	
	print("Finished")
	wait(5)
end
1 Like

With the parts that I need, where exactly would I place those parts inside of my script? @MayorGnarwhal

Well you already loop through all the players to teleport them, so add them to the table and create the Humanoid.Died connection inside that loop. Create the playersInRound and connections tables just before then. Have the player removed connection just after that. Then when you have the round countdown, add a condition so the countdown is stopped when Countdown <= 0 or #playersInRound == 0

The example code I gave you closely follows your round system structures, so just keep the parts in that order.

1 Like

@MayorGnarwhal I’ve tried to fix it but I’m a bit new to scripting… sorry… but this made the whole system stop working… heres the script (I think I did something wrong…

ServerStorage = game:GetService("ServerStorage")
ReplicatedStorage = game:GetService("ReplicatedStorage")
Players = game:GetService("Players")

Maps = ServerStorage:WaitForChild('Maps'):GetChildren()
Status = ReplicatedStorage:WaitForChild('Status')

while true do
	
	--Intermission
	
	local Countdown = 30 -- intermission, make this as long as you want
	
	repeat wait(1)
		Countdown = Countdown - 1
		
		Status.Value = 'Intermission : '..Countdown
	until Countdown <= 0
	
	--Choose the map.
	
	Status.Value = 'Choosing Map...'
	
	local ChosenMap = Maps[math.random(1, #Maps)]:Clone()
	local Spawns = ChosenMap:FindFirstChild('Spawns'):GetChildren()
	local RandomSpawn = Spawns[math.random(1, #Spawns)]
	
	wait(5) -- little pause, make this as long as you want
	
	ChosenMap.Parent = workspace
	Status.Value = 'Map chosen, teleporting players.'
	
	wait(2) -- little pause, make this as long as you want
	local Knife = ServerStorage.Knife
	--teleport the players
	
	for _, Player in pairs(Players:GetChildren())do
		if Player.Character and Player.Character:FindFirstChild('Humanoid') then
			Player.Character.HumanoidRootPart.CFrame = RandomSpawn.CFrame
local newKnife = Knife:Clone()
newKnife.Parent = Player.Backpack
		end
	end
	
	Countdown = 5 -- Starting Round In, make this as long as you want
	
	repeat wait(1)
		Countdown = Countdown - 1
		
		Status.Value = 'Starting Round in : '..Countdown
	until Countdown <= 0
	
	Countdown = 60 -- Game Time
	
	repeat wait(1)
		Countdown = Countdown - 1
		
		Status.Value = 'Ingame : '..Countdown
	until Countdown <= 0
-- add players to the table
	local playersInRound = {} -- track alive players
	local connections = {} -- store connections to disconnect later
	for index, player in pairs(game.Players:GetPlayers()) do
		table.insert(playersInRound, player) -- add all players to table

		connections[player.Name] = player.Character.Humanoid.Died:Connect(function() -- remove player from list on death
			table.remove(playersInRound, table.find(playersInRound, player))
		end)
	end
	
	connections["Removing"] = game.Players.PlayerRemoving:Connect(function(player) -- remove player from list if they leave
		local ind = table.find(playersInRound, player)
		if ind then
			table.remove(playersInRound, ind)
		end
	end)
	
	for i = 1, 15 do
		print("Playing round", i)
		if #playersInRound == 0 then
			break
		end
		wait(1)
	end
	
	for index, connection in pairs(connections) do -- disconnect connections to prevent memory leaks
		connection:Disconnect()
	end
	
	print("Finished")
	wait(5)
end
	
	--Kill the players
	
	for _, Player in pairs(Players:GetChildren())do
		if Player.Character and Player.Character:FindFirstChild('Humanoid') then
			Player.Character.Humanoid:TakeDamage(2000)
		end
	end
	
	ChosenMap:Destroy()
	
	Status.Value = 'Round Ended, waiting for new game.'
	
	wait(4) -- little pause, make this as long as you want.
	
end

@Clueless_Brick

Try this

ServerStorage = game:GetService("ServerStorage")
ReplicatedStorage = game:GetService("ReplicatedStorage")
Players = game:GetService("Players")

Maps = ServerStorage:WaitForChild('Maps'):GetChildren()
Status = ReplicatedStorage:WaitForChild('Status')

while true do
	
	--Intermission
	
	local Countdown = 30 -- intermission, make this as long as you want
	
	repeat wait(1)
		Countdown = Countdown - 1
		
		Status.Value = 'Intermission : '..Countdown
	until Countdown <= 0
	
	--Choose the map.
	
	Status.Value = 'Choosing Map...'
	
	local ChosenMap = Maps[math.random(1, #Maps)]:Clone()
	local Spawns = ChosenMap:FindFirstChild('Spawns'):GetChildren()
	local RandomSpawn = Spawns[math.random(1, #Spawns)]
	
	wait(5) -- little pause, make this as long as you want
	
	ChosenMap.Parent = workspace
	Status.Value = 'Map chosen, teleporting players.'
	
	wait(2) -- little pause, make this as long as you want
	local Knife = ServerStorage.Knife
	--teleport the players
	
	local playersInRound = {} -- track alive players
	local connections = {} -- store connections to disconnect later
	for _, Player in pairs(Players:GetChildren())do
		if Player.Character and Player.Character:FindFirstChild('Humanoid') then
			Player.Character.HumanoidRootPart.CFrame = RandomSpawn.CFrame
			local newKnife = Knife:Clone()
			newKnife.Parent = Player.Backpack
			
			table.insert(playersInRound, Player) -- add all players to table
			connections[Player.Name] = Player.Character.Humanoid.Died:Connect(function() -- remove player from list on death
				table.remove(playersInRound, table.find(playersInRound, Player))
			end)
		end
	end
	
	connections["Removing"] = game.Players.PlayerRemoving:Connect(function(player) -- remove player from list if they leave
		local ind = table.find(playersInRound, player)
		if ind then
			table.remove(playersInRound, ind)
		end
	end)
	
	Countdown = 5 -- Starting Round In, make this as long as you want
	
	repeat wait(1)
		Countdown = Countdown - 1
		
		Status.Value = 'Starting Round in : '..Countdown
	until Countdown <= 0
	
	Countdown = 60 -- Game Time
	
	repeat wait(1)
		Countdown = Countdown - 1
		
		Status.Value = 'Ingame : '..Countdown
	until Countdown <= 0 or #playersInRound == 0
	
	--Kill the players
	for _, connection in pairs(connections) do -- disconnect connections to prevent memory leaks
		connection:Disconnect()
	end
	
	for _, Player in pairs(playersInRound)do
		Player:LoadCharacter()
	end
	
	ChosenMap:Destroy()
	
	Status.Value = 'Round Ended, waiting for new game.'
	
	wait(4) -- little pause, make this as long as you want.
	
end
2 Likes