How To Make A Round Stop When All Players Have Died?

Hello there!

I’m trying to make a round system. I got it working but I want to ask, how do I make a round stop when all players have died? This is my attempt on it, but the round doesn’t even start.

local getFrame = game.StarterGui.IntermissionGUI.Frame
local roundTime = 100
local clientStarted = game.ReplicatedStorage.ClientStarted
local clientEnded = game.ReplicatedStorage.ClientEnded
local playersInRound = {} 
local isOverrided = false
local getPlayers = game:GetService("Players")

while (#playersInRound ~= 0 and wait()) do
	wait(13)
	for _, plr in pairs(game.Players:GetChildren()) do
		if plr.Character:FindFirstChild("HumanoidRootPart") then
			plr.Character.HumanoidRootPart.CFrame = CFrame.new(53.55, 15.55, 57.1)
		end
	end
	clientStarted:FireAllClients()
	
	-- [PLAYER SYSTEM]
	
	local playersInRound = {}
	local playersDisconnected = {}
	
	for _, Player in pairs(getPlayers:GetChildren()) do
		if Player.Character and Player.Character:FindFirstChild('Humanoid') then

			table.insert(playersInRound, Player)
			playersDisconnected[Player.Name] = Player.Character.Humanoid.Died:Connect(function()
				table.remove(playersInRound, table.find(playersInRound, Player))
			end)
		end
	end
	
	playersDisconnected["Removing"] = game.Players.PlayerRemoving:Connect(function(player)
		local ind = table.find(playersInRound, player)
		if ind then
			table.remove(playersInRound, ind)
		end
	end)
	
	if (#playersInRound == 0 and wait()) then
		print("Everybody died!")
	else
		wait(100)
		clientEnded:FireAllClients()
		for _, plr in pairs(game.Players:GetChildren()) do
			if plr.Character:FindFirstChild("HumanoidRootPart") then
				plr.Character.HumanoidRootPart.CFrame = CFrame.new(53.55, 69.25, -113.35)
			end
		end	
	end
end

I think it’s because of the while (#playersInRound ~= 0 and wait()) do that I added, but I don’t know how to do it in another way.

1 Like
#playersInRound ~= 0

This should suffice, wait() returns how much time it has waited which in your case is nil because you’ve called the function without passing any values as arguments to it, as nil is falsy the condition of that while loop isn’t satisfied.

1 Like
while wait() do
	if #playersInRound <= 0 then
		print("No more players left!")
		break --break out of entire loop
	end

You can replace the break statement with a continue statement if you wish for the while loop to skip to the next iteration cycle. The break statement would cause the entire while loop to terminate.

1 Like

If the break statement causes it to terminate the entire loop, does that mean that the round system won’t work again? By that I mean like it will run a single round and when all players die it just breaks the entire system and it won’t work again?

I just read what you said again, are you saying that I can just replace break with continue to restart the round?

Yes, a continue statement will break the current iteration cycle and continue on with the next thus starting a new round.

I tried to do what you told me but, it doesn’t end or print out anything when I die. I probably misunderstood something or put something in the wrong place.

local getFrame = game.StarterGui.IntermissionGUI.Frame
local roundTime = 100
local clientStarted = game.ReplicatedStorage.ClientStarted
local clientEnded = game.ReplicatedStorage.ClientEnded
local playersInRound = {} 
local isOverrided = false
local getPlayers = game:GetService("Players")

while true do
	wait(18)
	for _, plr in pairs(game.Players:GetChildren()) do
		if plr.Character:FindFirstChild("HumanoidRootPart") then
			plr.Character.HumanoidRootPart.CFrame = CFrame.new(53.55, 15.55, 57.1)
		end
	end
	clientStarted:FireAllClients()

	-- [PLAYER SYSTEM]

	local playersInRound = {}
	local playersDisconnected = {}

	for _, Player in pairs(getPlayers:GetChildren()) do
		if Player.Character and Player.Character:FindFirstChild('Humanoid') then

			table.insert(playersInRound, Player)
			playersDisconnected[Player.Name] = Player.Character.Humanoid.Died:Connect(function()
				table.remove(playersInRound, table.find(playersInRound, Player))
			end)
		end
	end

	playersDisconnected["Removing"] = game.Players.PlayerRemoving:Connect(function(player)
		local ind = table.find(playersInRound, player)
		if ind then
			table.remove(playersInRound, ind)
		end
	end)
	
	while wait() do
		if #playersInRound <= 0 then
			print("No more players left! BRU HRURHRUHRRRHSAHSDHA")
			continue
		else
			wait(100)
			clientEnded:FireAllClients()
			for _, plr in pairs(game.Players:GetChildren()) do
				if plr.Character:FindFirstChild("HumanoidRootPart") then
					plr.Character.HumanoidRootPart.CFrame = CFrame.new(53.55, 69.25, -113.35)
				end
			end	
		end
	end
end

You don’t need a nested while true loop, just have it be part of the main loop itself.

Okay, I’m pretty sure I did everything correctly. When I die, it doesn’t print anything, end the round, or do anything. Did I do something wrong here?

local roundTime = 100
local clientStarted = game.ReplicatedStorage.ClientStarted
local clientEnded = game.ReplicatedStorage.ClientEnded
local playersInRound = {} 
local isOverrided = false
local getPlayers = game:GetService("Players")

while true do
	wait(18)
	for _, plr in pairs(game.Players:GetChildren()) do
		if plr.Character:FindFirstChild("HumanoidRootPart") then
			plr.Character.HumanoidRootPart.CFrame = CFrame.new(53.55, 15.55, 57.1)
		end
	end
	clientStarted:FireAllClients()

	-- [PLAYER SYSTEM]

	local playersInRound = {}
	local playersDisconnected = {}

	for _, Player in pairs(getPlayers:GetChildren()) do
		if Player.Character and Player.Character:FindFirstChild('Humanoid') then

			table.insert(playersInRound, Player)
			playersDisconnected[Player.Name] = Player.Character.Humanoid.Died:Connect(function()
				table.remove(playersInRound, table.find(playersInRound, Player))
			end)
		end
	end

	playersDisconnected["Removing"] = game.Players.PlayerRemoving:Connect(function(player)
		local ind = table.find(playersInRound, player)
		if ind then
			table.remove(playersInRound, ind)
		end
	end)
	
	if #playersInRound <= 0 then
		print("No more players left! BRU HRURHRUHRRRHSAHSDHA")
		continue
	else
		wait(100)
		clientEnded:FireAllClients()
		for _, plr in pairs(game.Players:GetChildren()) do
			if plr.Character:FindFirstChild("HumanoidRootPart") then
				plr.Character.HumanoidRootPart.CFrame = CFrame.new(53.55, 69.25, -113.35)
			end
		end	
	end
end



local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")
local clientStarted = RS:WaitForChild("ClientStarted")
local clientEnded = RS:WaitForChild("ClientEnded")

local playersInRound = {}
local playersDisconnected = {}
local playerDies = {}
local lobbyTime = 20
local roundTime = 100
local isOverrided = false
local inRound = false

Players.PlayerRemoving:Connect(function(Player)
	table.insert(playersDisconnected, Player)
	if table.find(playersInRound, Player) then
		table.remove(playersInRound, table.find(playersInRound, Player))
		if #playersInRound == 0 then
			print("Everyone has died!")
			inRound = false
		end
	end
end)

local function doRound()
	inRound = true
	playersInRound = {}
	playersDisconnected = {}
	
	task.wait(lobbyTime)
	
	clientStarted:FireAllClients()
	for _, Player in ipairs(Players:GetPlayers()) do
		local Character = Player.Character
		local Humanoid = Character:WaitForChild("Humanoid")
		local HRP = Character:WaitForChild("HumanoidRootPart")
		HRP.CFrame = CFrame.new(0, 0, 0) --good spawn location
		table.insert(playersInRound, Player)
		Humanoid.Died:Connect(function()
			table.insert(playersDisconnected, Player.Name)
			table.remove(playersInRound, table.find(playersInRound, Player))
			if #playersInRound == 0 then
				print("Everyone has died!")
				inRound = false
			end
		end)
	end
	
	task.wait(roundTime)
	
	clientEnded:FireAllClients()
	for _, Player in ipairs(Players:GetPlayers()) do
		local Character = Player.Character
		local Humanoid = Character:WaitForChild("Humanoid")
		local HRP = Character:WaitForChild("HumanoidRootPart")
		HRP.CFrame = CFrame.new(0, -500, 0) --void depth
	end
	inRound = false
end

task.spawn(function()
	while task.wait() do
		if not inRound then
			doRound()
		end
	end
end)
5 Likes

It works, thank you so much man. <3