How would I break this touched function?

if script.Parent.Parent.Parent == game.Workspace then
	debounce2 = false
else
	debounce2 = true
	script.Parent.Equipped:Connect(function(m)
		local debounce2 = false
		local plr = script.Parent.Parent
		local debounce = false
		if not debounce or debounce2 then 
			debounce2 = true
			debounce = true
			plr.Humanoid.Touched:Connect(function()
				script.Parent.Sound:Play()
				wait(.1)
				return 
			end)
		end
	end)
	script.Parent.Unequipped:Connect(function(m) 
		local debounce2 = true
		local debounce = true
	end)
end

It’s a script that allows for the player’s body parts to make a tapping sound whenever the tool is activated

you can make the .Touched function a variable then use :Disconnect() to stop it when you want
example:
touch = plr.Humanoid.Touched:Connect(function()

in unequipped section

touch:Disconnect()

type return to stop the function

If you want it so that you don’t want the function to ever fire again, have a connection and disconnect it.

Change

plr.Humanoid.Touched:Connect(function()

to

local touchConnection
touchConnection = plr.Humanoid.Touched:Connect(function()
touchConnection:Disconnect()

How would I disconnect the function within separate function? (I want it to disconnect when the tool is unequipped)

I edited the script for your specifications:

if script.Parent.Parent.Parent == game.Workspace then
	debounce2 = false
else
	debounce2 = true
	local humTouchConnection -- so unequip function can access
	script.Parent.Equipped:Connect(function(m)
		local debounce2 = false
		local plr = script.Parent.Parent
		local debounce = false
		if not debounce or debounce2 then 
			debounce2 = true
			debounce = true
			humTouchConnection = plr.Humanoid.Touched:Connect(function()
			-- set connection
				script.Parent.Sound:Play()
				wait(.1)
				return 
			end)
		end
	end)
	script.Parent.Unequipped:Connect(function(m) 
		-- disconnect
		if humTouchConnection ~= nil then
			humTouchConnection:Disconnect()
		end
		local debounce2 = true
		local debounce = true
	end)
end
2 Likes