Function firing more than once

Hi, I have a script that gives you points based on your damage and for some reason it’s firing more than once when it fires the second time. I have tested the script that gives the tag and it only fires once while this fires multiple times. I have tried adding a debounce, but it didn’t really work. Are there any ways to fix this?

		humanoid.ChildAdded:Connect(function()
			if humanoid:FindFirstChild("attacker") then
				local attacker = humanoid.attacker.Value
				local dmgTable = {}				
				local oldHp = humanoid.Health
				
				table.insert(dmgTable, attacker)	
				
				humanoid.HealthChanged:Connect(function()
					local damage = oldHp - humanoid.Health
					if humanoid.Health < oldHp then
						for _, added in pairs(dmgTable) do
							local dmgPoints = added.Character.Humanoid:FindFirstChild("damagePoints")		
							dmgPoints.Value += damage
							oldHp = humanoid.Health

							print(attacker, dmgPoints.Value)
						end
						dmgTable = {}
					end
				end)
			end
		end)

hey, the .HealthChanged function is inside .ChildAdded function, which means, whenever a new attacker is added, a new .HealthChanged function starts, which means, when the HealthChanges, it will trigger both of the functions, or more, if there were more attackers added to the humanoid (and more .HealthChanged functions started)

Seperate the .ChildAdded and .HealthChanged, this will fix your issue.

Hope this helped!

1 Like