Humanoid.Died event firing repeatedly

I’m trying to detect when a player has died and remove them from a table. However, the approach I’m using fires multiple times (Mostly 4 times). Is there a way I can get this to only fire once?

A temporary approach that works is:

  • Creating an “InRound” variable for the player in Script 1
  • Detecting when the player has died in a Script 2 and deleting “InRound”
  • Checking to see if “InRound” is present in Script 1. If not, presume they’re dead

Instead, I would like to do everything in one main script if possible.


Here’s my code:
Note - Although I’m using for loops, I’m quite certain that this is not the underlying problem.

“creator” is the killfeed from the original Roblox Sword, it’s an ObjectValue of a player

for x = RoundTime, 0, -1 do
	Status.Value = "Round - "..TFM:Convert(x, "Default", true) -- Time formatting module (110  --> 01:50)
		
	
	-- Checks if they're still playing
	for i, Player in pairs(AlivePlayers) do
		local Humanoid = Player.Character:WaitForChild("Humanoid")

		Humanoid.Died:Connect(function()
			
			if Humanoid:FindFirstChild("creator") then
				-- They were killed by another player
				
				local ResponsiblePlayer = Humanoid.creator.Value
				
				ResponsiblePlayer.leaderstats.Tokens.Value += 5
				print(Player.Name.." was killed by "..ResponsiblePlayer.Name)
				
			else
				-- They just died
				print(Player.Name.." is dead")
			end
	
			table.remove(AlivePlayers, i)
		end)
	end
3 Likes

It fires multiple times because you are connecting the event every iteration of the loop. You should either move the Humanoid.Died:Connect() code above the loop, or disconnect all the events before reconnecting.

3 Likes

Hello there is a problem with your dont use Humanoid.Died function. Use “if humanoid.health = 0”

3 Likes

Thanks, works perfectly!

Why does this work though?

1 Like

I for the life of me cannot figure out how to discconect an event. I’ve tried all of the following and get an error every single time

Humanoid.Died:Connect(function()
    Humanoid.Died:Disconnect()

    -- Rest of Code
end)
Humanoid.Died:Connect(function()
    Humanoid:Disconnect()

    -- Rest of Code
end)
DeadPlayer = Humanoid.Died:Connect(function()
    DeadPlayer:Disconnect()

    -- Rest of Code
end)
local DeadPlayer = Humanoid.Died:Connect(function()
    DeadPlayer:Disconnect()

    -- Rest of Code
end)

This Should clear stuff out a bit (I haven’t run these but they should be fine)

--> First Approach With .Health
for x = RoundTime, 0, -1 do
	Status.Value = "Round - "..TFM:Convert(x, "Default", true) -- Time formatting module (110  --> 01:50)


	-- Checks if they're still playing
	for i, Player in pairs(AlivePlayers) do
		local Humanoid = Player.Character:WaitForChild("Humanoid")
		if Humanoid.Health <= 0 then
			table.remove(AlivePlayers,i)
			if Humanoid:FindFirstChild("creator") then
				-- They were killed by another player

				local ResponsiblePlayer = Humanoid.creator.Value

				ResponsiblePlayer.leaderstats.Tokens.Value += 5
				print(Player.Name.." was killed by "..ResponsiblePlayer.Name)

			else
				-- They just died
				print(Player.Name.." is dead")
			end
		end
	end
end

--> Second Approach with a Global Table of Players Alive
local AlivePlayers = {} --> Store Somehow Maybe by Looping through them all Once
for i, Player in pairs(AlivePlayers) do
	local Humanoid = Player.Character:WaitForChild("Humanoid")
	Humanoid.Died:Connect(function()

		if Humanoid:FindFirstChild("creator") then
			-- They were killed by another player

			local ResponsiblePlayer = Humanoid.creator.Value

			ResponsiblePlayer.leaderstats.Tokens.Value += 5
			print(Player.Name.." was killed by "..ResponsiblePlayer.Name)

		else
			-- They just died
			print(Player.Name.." is dead")
		end

		table.remove(AlivePlayers, i)
	end)
end
1 Like

If the code is firing Died more than once all you really need to do is use Once:

Once is a Method for RBXScriptSignals which returns a RBXScriptConnection which also automatically disconnects itself whenever it is fired once.

Humanoid.Died:Once(function()
      --code
end)
2 Likes
local Died = Humanoid.Died:Connect(function()
    
end)
Died:Disconnect()

Never heard that before! Will definetely keep that in mind for future projects.

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