Player Death/Leaving detection not accurate

I’m making a minigames system, The player death & leaving detection system isnt entirely accurate sometimes, i’m wonder if i did anything wrong…

Basically what happens, is when a player loses/leaves, they SHOULD get removed from the table, but the win-detection system still detects them in the current players alive table

local PlayersTable = {}
local LeaveConnections = {}
local DeathConnections = {}
function GetPlayers()
	for i,Player in pairs(Players:GetPlayers()) do
		local Index = i
		--if Player then
		--print(Player)
		local Name = Player.Name
		local Humanoid = Player.Character:FindFirstChildOfClass("Humanoid")
		
		PlayersTable[Index] = Player
		LeaveConnections[Index] = Players.PlayerRemoving:Connect(function(Plr)
			if Plr.Name == Name then
				if DeathConnections[Index] then DeathConnections[Index]:Disconnect();table.remove(DeathConnections,Index) end
				table.remove(PlayersTable,Index)
			end
		end)
		DeathConnections[Index] = Humanoid.HealthChanged:Connect(function() --// NOTE, IVE USED .Died BUT NOTHING REALLY CHANGED
			if Humanoid.Health <= 0 then
				if DeathConnections[Index] then DeathConnections[Index]:Disconnect();table.remove(DeathConnections,Index) end
				table.remove(PlayersTable,Index)
				table.remove(DeathConnections,Index)
			end
		end)
		--end
	end
	return PlayersTable
end

Things to note:

  • I’ve Cleaned/Reset the PlayerTable when the round is finished
  • I’ve Used Humanoid.Died

When you use table.remove and table.insert, the indexes change, therefore, your index may be inaccurate (another player).

You can use table.find to get the index of the player in the table, then remove it.

Also, to make it easier, you can use Player.CharacterRemoving to detect when the character is gone (after death and leave).

would i do something like this then?
table.remove(PlayersTable,table.find(Player.Name,PlayersTable))?

Like this:

table.remove(PlayersTable,table.find(PlayersTable, Player.Name))

As the first parameter is the table.

But I suggest you to check first if it finds anything, as you can’t remove nil from the table, it’ll error.

1 Like
local PlayersTable = {}
local LeaveConnections = {}
local DeathConnections = {}
function GetPlayers()
	for i,Player in pairs(Players:GetPlayers()) do
		local Index = i
		--if Player then
		--print(Player)
		local Name = Player.Name
		local Humanoid = Player.Character:FindFirstChildOfClass("Humanoid")
		
		PlayersTable[Index] = Player
		
		Index = table.find(PlayersTable,Player.Name)
		
		if not Index then continue end
		
		LeaveConnections[Index] = Players.PlayerRemoving:Connect(function(Plr)
			if Plr.Name == Name then
				if DeathConnections[Index] then DeathConnections[Index]:Disconnect();table.remove(DeathConnections,Index) end
				if LeaveConnections[Index] then LeaveConnections[Index]:Disconnect();table.remove(LeaveConnections,Index) end
				pcall(function()
					table.remove(PlayersTable,Index)
				end)
			end
		end)
		DeathConnections[Index] = Humanoid.HealthChanged:Connect(function()
			if Humanoid.Health <= 0 then
				if DeathConnections[Index] then DeathConnections[Index]:Disconnect();table.remove(DeathConnections,Index) end
				if LeaveConnections[Index] then LeaveConnections[Index]:Disconnect();table.remove(LeaveConnections,Index) end
				pcall(function()
					table.remove(PlayersTable,Index)
					table.remove(DeathConnections,Index)
				end)
			end
		end)
		--end
	end
	return PlayersTable
end

How’s this?

You need to get the Index right before you remove it from the table, so it gets the right index.
Like this:

local PlayersTable = {}
local LeaveConnections = {}
local DeathConnections = {}
function GetPlayers()
	for i,Player in pairs(Players:GetPlayers()) do
		local Index = i
		--if Player then
		--print(Player)
		local Name = Player.Name
		local Humanoid = Player.Character:FindFirstChildOfClass("Humanoid")
		
		PlayersTable[Index] = Player
				
		if not Index then continue end
		
		LeaveConnections[Index] = Players.PlayerRemoving:Connect(function(Plr)
			if Plr.Name == Name then
--------------------------------------
				Index = table.find(PlayersTable, Name )
--------------------------------------
				if DeathConnections[Index] then DeathConnections[Index]:Disconnect();table.remove(DeathConnections,Index) end
				if LeaveConnections[Index] then LeaveConnections[Index]:Disconnect();table.remove(LeaveConnections,Index) end
				pcall(function()
					table.remove(PlayersTable,Index)
				end)
			end
		end)
		DeathConnections[Index] = Humanoid.HealthChanged:Connect(function()
			if Humanoid.Health <= 0 then
--------------------------------------
				Index = table.find(PlayersTable, Name)
--------------------------------------
				if DeathConnections[Index] then DeathConnections[Index]:Disconnect();table.remove(DeathConnections,Index) end
				if LeaveConnections[Index] then LeaveConnections[Index]:Disconnect();table.remove(LeaveConnections,Index) end
				pcall(function()
					table.remove(PlayersTable,Index)
					table.remove(DeathConnections,Index)
				end)
			end
		end)
		--end
	end
	return PlayersTable
end
2 Likes