Why does my humanoid.Died() event happen more than once?

I was making a sword for my game and basically everything works except for when you kill someone, your kills keep adding more than 1 kill to your leaderstats. For example, if I killed one player, it would set my kills to 5 instead of one. Anybody able to help?

handle.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if properties["Can Damage"] == true then
			properties["Can Damage"] = false
			local attacker = character
			local victim = hit.Parent
			if attacker ~= victim then 
				if victim.Humanoid.Health >0 then 
					if marketPlaceService:UserOwnsGamePassAsync(player.UserId,22882690) then
						victim.Humanoid.Health = victim.Humanoid.Health - properties.Damage * 2
					else
						victim.Humanoid.Health = victim.Humanoid.Health - properties.Damage
					end
					local obj = victim.Humanoid
					obj.Died:Connect(function()
						player.leaderstats.KOs.Value = player.leaderstats.KOs.Value + 1
						player.leaderstats.Streak.Value = player.leaderstats.Streak.Value + 1

						print(victim.Name.." has died")
						game.ReplicatedStorage:FindFirstChild("ViewKiller"):FireClient(players:GetPlayerFromCharacter(victim),player)
					end)
				end
			end
		end
	end
end)
2 Likes

U could simply add a debounce

db = false -- above the code

---in the died event
if db == false then
db = true

end
1 Like

Because .Touched will happen each time a Part touches the item. This can happen a dozen times almost instantly when a player touches a Part.
Just as @IcyMizu stated, use a debounce.

3 Likes

I did, that’s what Can Damage is for

I would put the debounce in the died event

I’ve added a debounce which is what Can Damage is for :confused: Should I move the line where I check if can damage is true further down my code ?

Actually you should Disconnect this event obj.Died
also you forgot to set properties["Can Damage"] == true

I’ve been changing the can damage property when the player activates the tool, that’s not the entire code.

U should check the candamage value in the died event so every time the died event triggers it sees if the candamage value is true or not and if it is it can pass if it isn’t it wont pass

if conn then conn:Disconnect() end
local conn = obj.Died

So where is your CanDamage set to true again?

When the player activates the tool, I change the CanDamage property. I didn’t post the part where the player activates the tool in the code

It should be reset to true at the end of the .Touched function with a short wait(x) just before it, otherwise the touch will keep repeating in that split second that it takes until it’s reset to true. That’s what a Debounce Patterns | Roblox Creator Documentation is supposed to counteract.
(edited)

This doesn’t work, could you explain what I need to do?

well you know every time kill some one , you create another event obj.Died. This event will stay forever if you did not disconnect it, then it get double every time you kill some one.

image
When I added the code you sent, I got this :confused:

local obj = victim.Humanoid
local conn = obj.Died()
if conn then
	conn:Disconnect()
end

try this code :

local conn = nil
handle.Touched:Connect(function(hit)
	if conn then conn:Disconnect() end
	if hit.Parent:FindFirstChild("Humanoid") then
		if properties["Can Damage"] == true then
			properties["Can Damage"] = false
			local attacker = character
			local victim = hit.Parent
			if attacker ~= victim then 
				if victim.Humanoid.Health >0 then 
					if marketPlaceService:UserOwnsGamePassAsync(player.UserId,22882690) then
						victim.Humanoid.Health = victim.Humanoid.Health - properties.Damage * 2
					else
						victim.Humanoid.Health = victim.Humanoid.Health - properties.Damage
					end
					local obj = victim.Humanoid
					conn = obj.Died:Connect(function()
						player.leaderstats.KOs.Value = player.leaderstats.KOs.Value + 1
						player.leaderstats.Streak.Value = player.leaderstats.Streak.Value + 1

						print(victim.Name.." has died")
						game.ReplicatedStorage:FindFirstChild("ViewKiller"):FireClient(players:GetPlayerFromCharacter(victim),player)
					end)
				end
			end
			wait(.1)
			properties["Can Damage"] == true
		end
	end
end)
1 Like

It won’t work now, it won’t output anything either this time.

It’s complicated :sweat_smile:
###########

That’s okay, I think I have a solution to it, I’ll accept your answer if you’d like