What to do if the script removes from the table more players place 1 player?

The script removes from the table more players place 1 player.

Script:

while RoundTime >= 0 and RoundNow == true do
for _,plr in pairs(game.Players:GetChildren()) do
	if plr.PlayerGui:FindFirstChild("GameGui") then
		plr.PlayerGui:FindFirstChild("GameGui").InfoText.Text = "Time Left: " .. RoundTime					
	end					
		local hum = plr.Character.Humanoid
		local connection
			connection = hum.Died:Connect(function(dead)
				table.remove(AlivePlayers,table.find(AlivePlayers,plr.Name))
				connection:Disconnect()
				plr.PlayerGui.GameGui.ButtonsFrame.SpectateButton.Visible = true		
			end)
		print(#AlivePlayers)
end
		RoundTime -= 1	
		print(RoundTime)
		wait(1)		
end		
1 Like

Looks like the problem might be that the script keeps connecting the “humanoid died” event over and over again inside the round loop.

Might be better to connect it just once, outside of the loop. So when a player dies, the script will only remove them from the table once instead of multiple times

Also you can use :Once instead of :Connect

-- Connect died events first
for _,plr in pairs(game.Players:GetChildren()) do
	local hum = plr.Character.Humanoid
	hum.Died:Once(function(dead)
		table.remove(AlivePlayers,table.find(AlivePlayers,plr.Name))
		plr.PlayerGui.GameGui.ButtonsFrame.SpectateButton.Visible = true		
	end)
end

-- Start round
while RoundTime >= 0 and RoundNow == true do
	for _,plr in pairs(game.Players:GetChildren()) do
		if plr.PlayerGui:FindFirstChild("GameGui") then
			plr.PlayerGui:FindFirstChild("GameGui").InfoText.Text = "Time Left: " .. RoundTime					
		end					
	end
	RoundTime -= 1	
	print(RoundTime)
	wait(1)		
end
1 Like