How do i make debounce work

hello i dont know much scripting but yea heres the script that i made:

local ButtonPart = script.Parent
local function Touchy()
local debounce = false
if ButtonPart.Touched then
if not debounce then
debounce = true
end
game.Workspace.Door2.Transparency = .5
game.Workspace.Door2.CanCollide = false
script.Parent.BrickColor = BrickColor.Red()
wait(2)
script.Parent.BrickColor = BrickColor.Green()
game.Workspace.Door2.Transparency = 0
game.Workspace.Door2.CanCollide = true
wait(5)
debounce = false

end
	end

ButtonPart.Touched:Connect(Touchy)

the problem is debounce doesnt work,did i do something wrong? please help

Try this

local ButtonPart = script.Parent
local debounce = false

local function Touchy(Object)
	if Object.Parent:FindFirstChild("Humanoid") then
		if not debounce then
			debounce = true
			game.Workspace.Door2.Transparency = .5
			game.Workspace.Door2.CanCollide = false
			ButtonPart.BrickColor = BrickColor.Red()
			task.wait(2)
			ButtonPart.BrickColor = BrickColor.Green()
			game.Workspace.Door2.Transparency = 0
			game.Workspace.Door2.CanCollide = true
			task.wait(5)
			debounce = false
		end
	end
end
ButtonPart.Touched:Connect(Touchy)

Explanation:

if Object.Parent:FindFirstChild("Humanoid") then
This line checks if the object that touched that part has a humanoid inside [only NPC’s and Players]

if not debounce then debounce = true
This line will occur only when debounce is equal to the opposite of what you defined it, in this case - true.

1 Like