Humanoid.Died function triggering more than once

local deathcount = 0
local debounce = false

game:GetService("Players").PlayerAdded:Connect(function(Player)
	game:GetService("ServerStorage").HitPerson.Event:Connect(function(Character)
		if Player then	
			if Player.Team == game:GetService("Teams")["A"] then
				Character.Humanoid.Died:Connect(function()
					if debounce == false then
						debounce = true
						deathcount = deathcount + 1
						print(deathcount)

						debounce = false

					end
				end)
			end
		end
	end)
end)

Its supposed to only trigger once, but it triggers more than once, and it can still trigger when someone hits the body too.

As far as I can tell, you’re connecting the event every time a player joins, so it will trigger once for every player that has joined.

Perhaps, get the Player using “GetPlayerFromCharacter” which, as it suggests, gets the player from the character. This doesn’t return any errors if the character is not valid, it’ll just return nil, so we can use it as such:

local Players = game:GetService("Players")
game:GetService("ServerStorage").HitPerson.Event:Connect(function(Character)
    local Player = Players:GetPlayerFromCharacter(Character)
    if Player then	
	    if Player.Team == game:GetService("Teams")["A"] then
	      Character.Humanoid.Died:Connect(function()
	          if debounce == false then
		    	  debounce = true
		    	  deathcount = deathcount + 1
		    	  print(deathcount)

		    	  debounce = false
              end
          end)
	   end
    end
end)

You are setting debounce to false every time. There’s no need to set it to false.

1 Like

That only makes it trigger once, I’m looking for it to trigger multiple times, but only once per death.

Still triggers multiple times per death.

I don’t think there’s particularly something wrong with your code, it’s just that there’s no logical use for to debounce here - try and implementing it, so the debounce is set to false after the character respawned.

Nevermind. My bad. You’re listening for the character’s death every time that event is fired.
You shouldn’t need to do that every time

You shouldn’t even need a debounce, you should connect a new function every time the character is added, so I’ve used “Once” which disconnects the function when it’s triggered. This should do what you want

local deathcount = 0

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		Character.Humanoid.Died:Once(function()
			if Player.Team == game:GetService("Teams")["A"] then
				deathcount = deathcount + 1
			end
		end)
	end)
end)

Also, we’re checking the team when they die rather than when they spawn. Hope this helps

The problem is they’re creating multiple connections for the same event. This is happening every time the player takes a hit.

The event fires only when the player’s Health is below 0. Every hit damages the player, therefore going under 0. But I think you’re right with the solution you posted a few minutes ago.

Yes that is correct, but since they’ve connected it to multiple functions, a single death will make it fire all of those functions (which are all identical, and add 1 to the players death)

1 Like

You’re detecting this on the server?

Humanoid events are notoriously difficult to connect server-side.

Consider detecting death on the client & firing a RemoteEvent to the server.

Is that feasible for your use-case?

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