Debounce not working?

When a player clicks the door, it should check if debounce is off, turn it’s Transparency’s value to 1, turn CanCollide off, play a sound and turn debounce on. After 5 seconds, it should turn the invisible door’s CanCollide off, while turning Transparency’s value to 0, play a sound, and turning on CanCollide

When I spam click, the sounds spam along, but it’s only the OpeningDoorSF (Sound Efffect) that’s being spammed.

No errors are present in the Output.

local closedDoor = script.Parent.Parent.DoorFrame 
local openedDoor = script.Parent.Parent.Parent.OpenedDoor.DoorFrame
local clickDetector = script.Parent.ClickDetector
local openedDoorSound = closedDoor.DoorOpeningSF
local closedDoorSound = closedDoor.DoorClosingSF

function onClick()
	local debounce = false
	if debounce == false then
		debounce = true
		closedDoor.Transparency = 1
		closedDoor.CanCollide = false
		openedDoor.Transparency = 0
		openedDoor.CanCollide = true	
		openedDoorSound:Play()
		wait(5)
		closedDoor.Transparency = 0
		closedDoor.CanCollide = true
		openedDoor.Transparency = 1
		openedDoor.CanCollide = false
		closedDoorSound:Play()
		debounce = false
	end
end

clickDetector.MouseClick:Connect(onClick)
1 Like

You are defining the debounce in the function so whenever it is called the debounce will always be false at the beginning.

This should work:

local closedDoor = script.Parent.Parent.DoorFrame 
local openedDoor = script.Parent.Parent.Parent.OpenedDoor.DoorFrame
local clickDetector = script.Parent.ClickDetector
local openedDoorSound = closedDoor.DoorOpeningSF
local closedDoorSound = closedDoor.DoorClosingSF
local debounce = false

function onClick()
	if debounce == false then
		debounce = true
		closedDoor.Transparency = 1
		closedDoor.CanCollide = false
		openedDoor.Transparency = 0
		openedDoor.CanCollide = true	
		openedDoorSound:Play()
		wait(5)
		closedDoor.Transparency = 0
		closedDoor.CanCollide = true
		openedDoor.Transparency = 1
		openedDoor.CanCollide = false
		closedDoorSound:Play()
		debounce = false
	end
end

clickDetector.MouseClick:Connect(onClick)
2 Likes