CanTouch/Debounce being weird?

Hello all! So today I’ve decided to make an FPS like knife for a game I’m making with a couple friends. Anyway, weapons aren’t really my strong suit, and I’m having trouble with a debounce system.
I decided to use the CanTouch property on parts to activate and deactivate my hitbox. The only problem is that it’s acting weird.
The debounce itself works, and hit’s get registered when I want them to. The only problem is that too many hits are registered, and they are chained in this sort of pattern. Every time a hit is registered, the next time I hit, the same amount of hits will register as the last hit plus one(1 hit registered, 3 hits registered, 6 hits registered, 10 hits registered etc.)

Thank you to anyone who is willing to help!

Here’s my code (Don’t mind the backstab stuff, that’s a work in progress):

--== values ==--

local debounce = false

local backstab = false

--== instance variables ==--
local Hitbox = script.Parent

--== main function ==--

script.Parent.Parent.Activated:Connect(function()
	
	if debounce == false then
		debounce = true
		Hitbox.CanTouch = true
		
		--== anim stuff over here ==--
		
		local hum = script.Parent.Parent.Parent.Humanoid
		
		local attack = hum:LoadAnimation(script.StabAnim)
		attack:Play()
		
		--== attack function stuff ==--
		
		local Humrp = script.Parent.Parent.Parent:FindFirstChild("HumanoidRootPart")
		
		Hitbox.Touched:Connect(function(hit)
			Hitbox.CanTouch = false
			if hit.Parent:FindFirstChild("Humanoid") then
				local EnemyHumanoid = hit.Parent.Humanoid
				if EnemyHumanoid.Parent:FindFirstChild("HumanoidRootPart") then
						local EnemyHumrp = EnemyHumanoid.Parent.HumanoidRootPart
						print("Hit is registered!")
					EnemyHumanoid:TakeDamage(50)
					--== backstab calculation ==--
				
				end
				
			end
		end)
		
		
		
		
		
		
		
		
		--== debounce stuff over here ==--
		wait(0.7)
		debounce = false
		
	else
		return
	end
end)

You’re not disconnecting the .Touched event after your attack is done. Consider moving the function to a variable then calling Function:Disconnect() after.

Alternatively, put the function outside the main part.

Thank you so much! That solution worked perfectly!

1 Like