Humanoid.Died event doesn't run when 2 players die at the same time

for i, player in pairs(Players) do
			local Humanoid = player.Character:WaitForChild("Humanoid")
			
			deathConnection = Humanoid.Died:Connect(function()
				print(player.Name .. " Died!")
				
				deathConnection:Disconnect()
				
				print("Connection disconnected")
				
				local Index = table.find(Connections, deathConnection)
				
				if Index ~= nil then
					table.remove(Connections, Index)
					print("Removed connection from connection table")
				end
				
				table.insert(PlayersWhoDied, 1, player)
				
				print("Added player to PlayerWhoDied table")
				
				local Index = table.find(Players, player)
				
				print("Index is " .. Index)
				
				if Index ~= nil then
					print("Index found!")
					table.remove(Players, Index)
					print("Removed " .. player.Name)
				end
			end)
			
			table.insert(Connections, deathConnection)
		end

This code works just fine and works very well when one player dies. The problem is, when two people die at the same time, the code only runs for one of the players. It’s as if it completely ignores that the second player died.

I loop through all the players in the round, setup died events for all of them, and when they die I run the code for my round winning system.

So the problem: When 2(maybe more) people die at the same time, only one of the events run.

Yes, It will not run because 1 script is checking all the players. What you can do is, move the script to StarterPlayerScripts. Since that will create the script for all players when they join.

Forgot to mention, you are gonna write a new script but it’s gonna check the player.

Well, it’s a module script. It’s in a function that sets up a winning detection system. And all the died events should be loaded by the time someone dies…

You have a problem with scope.

deathConnection should be created inside of the loop so that there’s a connection for each player’s humanoid. Right now, deathConnection is outside of the loop, so all of the connections are clashing because they’re all trying to use 1 global variable rather than their own.

Simply put, inside of the loop before anything is created write local deathConnection, so that one is created for every player

1 Like

I’m pretty sure this worked! Thank you!

1 Like

That doesn’t make sense. When a player died all the functions connected to the event are ran in a courintinite. I think you have a misunderstanding of how events work.

1 Like

Yeah, I was a bit confused on his comments.