Tool debounce not working correctly

  1. What do you want to achieve? Keep it simple and clear!
    A tool that when activated and hit a block it will decrease a value in it.

  2. What is the issue? Include screenshots / videos if possible!
    It is not working correctly, I activated the tool when it is not touching anything and when it touch it decrease the value for some reason

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried changing the debounce lines but won’t work

local canhit = true

script.Parent.Activated:Connect(function()
	script.Parent.Handle.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Break") then
			if canhit == true then
				canhit = false
				hit.Parent.Break.Value = hit.Parent.Break.Value - 1
				wait(0.5)
				canhit = true
			end
		end
	end)
end)

(I activated the tool only once at the start of the video but the tool start decreasing the value when touch but not activated)

This has nothing to do with your debounce. Everything about your debounce seems correct.

Your problem occurs when you enable the Touched event listener, which continuously waits for whenever the Touched event fires.

My suggestion is that you add a second debounce as shown below.

local canhit = true
local activated = false

script.Parent.Activated:Connect(function()
	activated = true
	script.Parent.Handle.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Break") then
			if canhit == true and activated == true then
				canhit = false
				hit.Parent.Break.Value = hit.Parent.Break.Value - 1
				wait(0.5)
				canhit = true
				activated = false
			end
		end
	end)
end)
2 Likes

Yep like @xendatro said the problem is with the touched event listener, once connected it doesn’t go away unless script is deleted or event is disconnected which both isn’t happening.

I believe a simpler approach is to connect the touched event seperately, then use activated to “enable” the touched event. Building upon @xendatro approach.

local toolActivated = false

local activatedDebounce = true
script.Parent.Activated:Connect(function()
	if activatedDebounce then
		activatedDebounce = false
		toolActivated = true
		task.wait(0.5)
		toolActivated = false
		activatedDebounce = true
	end
end)

--Will only trigger once before disabling itself
script.Parent.Handle.Touched:Connect(function(hit)
	if toolActivated and hit.Parent:FindFirstChild("Break") then
		hit.Parent.Break.Value = hit.Parent.Break.Value - 1
		toolActivated = false
	end
end)
1 Like
local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local touchedConn

tool.Activated:Connect(function()
	touchedConn = handle.Touched:Connect(function(hit)
		if debounce then
			return
		end
		debounce = true
		if hit.Parent:FindFirstChild("Break") then
			hit.Parent.Break.Value -= 1
		end
		task.wait(0.5)
		debounce = false
	end)
end)

tool.Deactivated:Connect(function()
	if touchedConn then
		touchedConn:Disconnect()
	end
end)

You’re connecting a callback function to the touched event of the tool’s handle each time the tool instance is activated (left mouse button clicked while equipped), as such it’s best to disconnect the event whenever the tool is subsequently deactivated.

Alternatively you could also achieve a functional debounce with the following.

local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local debounce = false
local canHit = false
local isEquipped = false

tool.Activated:Connect(function()
	canHit = true
end)

tool.Deactivated:Connect(function()
	canHit = false
end)

tool.Equipped:Connect(function()
	isEquipped = true
	canHit = false
end)

tool.Unequipped:Connect(function()
	isEquipped = false
	canHit = false
end)

handle.Touched:Connect(function(hit)
	if debounce then
		return
	end
	if not canHit then
		return
	end
	debounce = true
	if hit.Parent:FindFirstChild("Break") then
		hit.Parent.Break.Value -= 1
	end
	task.wait(0.5)
	debounce = false
end)