Round system bug

Hi! I’m working on a new game, and it features a round system that stops only when all the players in the current game have died. However, I’ve encountered a bug recently: if a player leaves during the round, they are still counted as “alive” by the game, causing the round not to stop when everybody else dies. I have no idea how to fix this issue and could really use some help.

Heres what I have so far:

function handleRound()
	
	local plrsAlive = {}
	for _, plr in pairs(game.Players:GetPlayers()) do
		
		if plr.Character and plr.Character.Humanoid.Health > 0 then
			table.insert(plrsAlive, plr)
			
			plr.Character.Humanoid.Died:Connect(function()
				table.remove(plrsAlive, table.find(plrsAlive, plr))
			end)
		end
	end
	
	for i = 1, 300 do
		task.wait(1)
		if #plrsAlive == 0 then
			break
		end
	end
	
	task.wait(5)
end

Thank you for reading all the way to the end, I appreciate the help.

use pcall() for rounds… np…

1 Like

Add a event for the player leaving using PlayerRemoving

plr.PlayerRemoving:Connect(function(player)
   table.remove(plrsAlive, table.find(plrsAlive, player))
end)
1 Like

Your code will error if the player wasn’t in the game when they left, because table.find will return nil since it won’t be able to find the player in the plrsAlive table.

Fixed code:

--//Services
local Players = game:GetService("Players")

--//Tables
local plrsAlive = {}

--//Functions
local function handleRound()
	--//Reset plrsAlive table
	table.clear(plrsAlive)
	
	for _, plr in ipairs(Players:GetPlayers()) do
		local character = plr.Character
		local humanoid = character and character:FindFirstChildWhichIsA("Humanoid")
		
		if humanoid and humanoid.Health > 0 then
			table.insert(plrsAlive, plr)

			humanoid.Died:Connect(function()
				table.remove(plrsAlive, table.find(plrsAlive, plr))
			end)
		end
	end

	for i = 1, 300 do
		task.wait(1)
		
		if #plrsAlive == 0 then
			break
		end
	end

	task.wait(5)
end

Players.PlayerAdded:Connect(function(player)
	local playerIndex = table.find(plrsAlive, player)
	
	if playerIndex then
		table.remove(plrsAlive, playerIndex)
	end
end)
1 Like

I tested it in roblox studio and it outputted no errors, but when me and a friend tested it, the round kept on going.

I don’t think so, since PlayerRemoving fires before the player leaves, and it the event returns the player.

Therefore, the event function would remove the player from the table.

1 Like

You don’t seem to understand what I mean.

Let’s say that the game already started, and in the middle of the round, a new person joins. That new person is not added to the plrsAlive table, so if he leaves before the round ends, your function will error because he was not part of that table in the first place.

1 Like

Oops, I meant to put PlayerRemoving instead of PlayerAdded, here:

--//Services
local Players = game:GetService("Players")

--//Tables
local plrsAlive = {}

--//Functions
local function handleRound()
	--//Reset plrsAlive table
	table.clear(plrsAlive)

	for _, plr in ipairs(Players:GetPlayers()) do
		local character = plr.Character
		local humanoid = character and character:FindFirstChildWhichIsA("Humanoid")

		if humanoid and humanoid.Health > 0 then
			table.insert(plrsAlive, plr)

			humanoid.Died:Connect(function()
				table.remove(plrsAlive, table.find(plrsAlive, plr))
			end)
		end
	end

	for i = 1, 300 do
		task.wait(1)

		if #plrsAlive == 0 then
			break
		end
	end

	task.wait(5)
end

Players.PlayerRemoving:Connect(function(player)
	local playerIndex = table.find(plrsAlive, player)

	if playerIndex then
		table.remove(plrsAlive, playerIndex)
	end
end)
1 Like

Thank you so much, you have absolutely no idea how much time you saved me!

Also thank you to @Korate168 and @M1Jy for responding, it means a lot!

1 Like

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