Debounce not working

I’ve been trying to make a debounce but it doesn’t work and I can’t find the issue with the script.

The script is inside a tool and the detector part is welded to the handle, the tool works if you remove the debounce lines, what happens in this case is that it starts working after 20 seconds (without the debounce)

 local debounce = false
script.Parent.Detector.Touched:Connect(function(hit)
	if not debounce then
		debounce = true
		local humm = hit.Parent:FindFirstChild("Humanoid")
		if humm then
			local getch = script.Parent:GetChildren()
			for i, v in pairs(getch) do
				if v.Name == "Button" then
					v.BrickColor = BrickColor.Random()
				end
			end
		end
	end
	wait(20)
	debounce = false
end)
 local debounce = false
script.Parent.Detector.Touched:Connect(function(hit)
	if debounce == false then 
		debounce = true
		local humm = hit.Parent:FindFirstChild("Humanoid")
		if humm then
			local getch = script.Parent:GetChildren()
			for i, v in pairs(getch) do
				if v.Name == "Button" then
					v.BrickColor = BrickColor.Random()
				end
			end
		end
	end
	wait(20)
	debounce = false
end)

Maybe try this, the only other thing could be that it doesn’t find the Humanoid. If this doesn’t work then let us know

It does the same thing, nothing changed, I think I might have to use a debounce function or something like that

You put these 2 lines outside the if statement. It must be inside the if statement so it works perfectly.

3 Likes