Knife doing too much damage per click?

I am scripting a knife that when and ONLY when clicked to do 35 damage to the player, this is one of the first times I’ve really done anything with tools and I’ve encountered an issue that I can’t solve.

Essentially whenever you click the knife it does more than 35 damage per click even with a cooldown / debounce.

I’ve tried a few different things like turning off cantouch or checking if anyone else has a better working solution to this, but I have not found anything yet.

local cooldown = false
local hc = true
script.Parent.Activated:Connect(function()
	if cooldown == false then
		script.Parent.Sound.SoundId = "rbxassetid://7048393965"
		cooldown = true
		local anim = script.Parent.Parent:FindFirstChild("Humanoid"):LoadAnimation(script.Parent.Hit)
		anim:Play()
		script.Parent.HitPart.Touched:Connect(function(h)
			if hc == false then
				hc = true
					if h.Parent:FindFirstChild("Humanoid") or h.Name == "Humanoid" then
					h.Parent.Humanoid:TakeDamage(35)
					script.Parent.Sound.SoundId = "rbxassetid://6330988021"
				end
			end
		end)
		script.Parent.Sound:Play()
		anim.Stopped:Wait()
		wait(0.5)
		hc = false
		cooldown = false
	end
end)

alright, this is because hc will always be false in this script and the touched event always runs once you activate the tool once. there are 2 ways to fix this.

  1. You change this to hc = true and make it false before the you use the .Touched event.
  1. use a connection and disconnect after touch, not needing hc.
local cooldown = false
script.Parent.Activated:Connect(function()
	if cooldown == false then
		script.Parent.Sound.SoundId = "rbxassetid://7048393965"
		cooldown = true
		local anim = script.Parent.Parent:FindFirstChild("Humanoid"):LoadAnimation(script.Parent.Hit)
		anim:Play()
		local hitconnection = script.Parent.HitPart.Touched:Connect(function(h)
			if h.Parent:FindFirstChild("Humanoid") or h.Name == "Humanoid" then
			h.Parent.Humanoid:TakeDamage(35)
			script.Parent.Sound.SoundId = "rbxassetid://6330988021"
			hitconnection:Disconnect()
		end)
		script.Parent.Sound:Play()
		anim.Stopped:Wait()
		wait(0.5)
		hitconnection:Disconnect()
		cooldown = false
	end
end)
2 Likes

It looks fine to me, at least from the video. In the video it took 3 hits, which would be a combined 105 health deduction, more than the 100 health players have.

But you’ll see in the video it was only from 2 clicks which should’ve only resulted in 70 damage total, the issue I’m encountering is that it’s doing additional damage per click, as in the video it did 105 damage instead of 70

I’ve never seen a use of hitconnection before but after a bit of debugging it seemed to work flawlessly, thank you for the help!

When ever you connect a function to an event it return the connection. So you can call your_connection_variable:Disconnect() when not in use.