Why is my debounce touch script not stopping?

I’m trying to get my script to trigger a while loop to stop, if another part is touching the main part

local part = script.Parent

debounce = true

if debounce == true then
	
	function onTouch(part)
		if part.Parent:WaitForChild("OtherPart") then
			print(part,"Touched")
			debounce = false
		end
	end
	 
	function onTouchEnded(part)
		if part.Parent:FindFirstChild("OtherPart") then
			print(part,"Touched Ended")
			--debounce = true is left like this for testing
		end
	end
	part.Touched:Connect(onTouch)
	part.TouchEnded:Connect(onTouchEnded)
end
	
while debounce == true do
	part.BrickColor = BrickColor.Random()
end

You have to use :Disconnect()

When a part is touched, disconnect the functions:

local Connection = part.Touched:Connect(onTouch)
Connection:Disconnect()

Have the if debounce == true after the Ontouch function.

I did not know this function existed and where would i put the connection:Disconnect()?

try if debounce then, that might work.

1 Like

Try local local debounce = true .

dont think this is gonna work but hey ho

this works but when the Otherpart TouchEnded the while loop still does not re-fire

Try this:

debounce = true
	
function onTouch(part)
    if debounce then
        if part.Parent:WaitForChild("OtherPart") then
	    print(part,"Touched")
            debounce = false
        end
    end
end
	 
function onTouchEnded(part)
    if part.Parent:FindFirstChild("OtherPart") then
	print(part,"Touched Ended")
	--debounce = true is left like this for testing
    end
end

part.Touched:Connect(onTouch)
part.TouchEnded:Connect(onTouchEnded)

while debounce do
	part.BrickColor = BrickColor.Random()
end

the while loop still does not refire if the wanted part stopped touching it

There’s any error in the output?

No, the part does stop changing colors when touched, but still does not continue to change colors if the touch is over

function onTouchEnded(part)
    if part.Parent:FindFirstChild("OtherPart") then
	print(part,"Touched Ended")
	debounce = true 
    end
end

You forgot to set debounce back to true, replace the toucheEnded function with this.

Hey there, sorry if it’s been a while and I don’t know if you solved this, here is a fix.

local part = script.Parent

debounce = false

function onTouch(part)
	if part.Parent:WaitForChild("OtherPart") then
		print(part,"Touched")
		debounce = true
	end
end
	 
function onTouchEnded(part)
	if part.Parent:FindFirstChild("OtherPart") then
		print(part,"Touched Ended")
		debounce = false
	end
end
part.Touched:Connect(onTouch)
part.TouchEnded:Connect(onTouchEnded)
	
while true do
	wait()
	if debounce == true then
		part.BrickColor = BrickColor.Random()
	end
end