Is there a way to detect if multiple players have died?

is there a way to detect if more than one player has died?

You can use a table to store when a player dies. Here’s a quick example of what that could look like:

local deadPlayers = {}

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        local humanoid = character:WaitForChild("Humanoid")
        humanoid.Died:Connect(function()
            table.insert(deadPlayers, player)
        end)
    end)
end)
3 Likes

You can use a function like this one I just wrote, to check how many players that are currently dead:

function GetDeadPlayers()
	local DeadPlayers = {}
	
	for index, player in pairs(game.Players:GetPlayers()) do
		local Character = player.Character
		
		if not Character then
			DeadPlayers[#DeadPlayers+1] = player
		elseif Character then
			if Character.Humanoid.Health <= 0 then
				DeadPlayers[#DeadPlayers+1] = player
			end
		end
	end
	
	return DeadPlayers 
end

while wait(1) do
	print(#GetDeadPlayers())
end
1 Like

You can also remove a player from the table if Player.CharacterAdded is called.

1 Like

How do I save the value of DeadPlayers because after a player dies it resets. So if a player dies it will say 1 but once the player spawns in again it says 0 and when you reset again it doesn’t say 2 it says 1.

My function is meant to be used to check which players are currently dead. For what you’re asking me about, @K_reex’s function is more suitable and intended for just that.

How would I use that as an if statement such as if the deadPlayers was 2 or greater do this?

If you want code to run after two or more players have died, your script should look something like this:

local deadPlayers = {}

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        local humanoid = character:WaitForChild("Humanoid")
        humanoid.Died:Connect(function()
            table.insert(deadPlayers, player)
            if deadPlayers[2] ~= nil then
               --The code you put here should run after every death except the first.
            end
        end)
    end)
end)
1 Like

You can access the number of objects in a table by using the # symbol.

ex:

local myTable = {"Apple", "Orange"}
print(#myTable) --> 2

You can then write an if statement to check if that is greater than 2. In your case:
if #deadPlayers >= 2 then ... end

I think you should check out the documentation on tables, they’re a really big part of Lua and can really help you out when you’re scripting.

1 Like