Hello, I have a secret/trapdoor that is intended to open when I click on, or touch (with my player) a book on a nearby shelf. When I click the book, everything works perfectly as intended. However, when I stand on the book, move left to right on the book, etc. the sound plays multiple times, sometimes the door opens only for a short moment and closes quickly again, and overall it just doesn't work as intended.
I'm sorry if my code looks poor, I am very new to programming and have been trying my best to figure this all out. Can anybody help me out with this?
In the past I have tried to add debounce but I didn't seem to do it correctly, if that is what I need here.
Here is the full script:
local Sound = game.Workspace.Map.Level1.BookTrapdoor.Trapdoor.DoorOpening local BookSwitch = game.Workspace.Map.Level1.BookTrapdoor.BookSwitch.Button local Trapdoor = game.Workspace.Map.Level1.BookTrapdoor.Trapdoor local ClickDetector = game.Workspace.Map.Level1.BookTrapdoor.BookSwitch.ClickDetector COOLDOWN = false COOLDOWN_TIME = 3 ClickDetector.MouseClick:Connect(function() if COOLDOWN == false then COOLDOWN = true end Sound:play() Trapdoor.Transparency = 0.8 Trapdoor.CanCollide = false ClickDetector.MaxActivationDistance = 0 wait(COOLDOWN_TIME) Trapdoor.Transparency = 0 Trapdoor.CanCollide = true ClickDetector.MaxActivationDistance = 10 COOLDOWN = false wait() end) BookSwitch.XPart.Touched:Connect(function() if COOLDOWN == false then COOLDOWN = true end Sound:play() Trapdoor.Transparency = 0.8 Trapdoor.CanCollide = false ClickDetector.MaxActivationDistance = 0 wait(COOLDOWN_TIME) Trapdoor.Transparency = 0 Trapdoor.CanCollide = true ClickDetector.MaxActivationDistance = 10 COOLDOWN = false Sound.Ended(wait) end)
Your cooldown variable is being changed but not being under the if. change the code to something like this:
local Sound = game.Workspace.Map.Level1.BookTrapdoor.Trapdoor.DoorOpening local BookSwitch = game.Workspace.Map.Level1.BookTrapdoor.BookSwitch.Button local Trapdoor = game.Workspace.Map.Level1.BookTrapdoor.Trapdoor local ClickDetector = game.Workspace.Map.Level1.BookTrapdoor.BookSwitch.ClickDetector COOLDOWN = false COOLDOWN_TIME = 3 ClickDetector.MouseClick:Connect(function() if COOLDOWN == false then COOLDOWN = true Sound:play() Trapdoor.Transparency = 0.8 Trapdoor.CanCollide = false ClickDetector.MaxActivationDistance = 0 wait(COOLDOWN_TIME) Trapdoor.Transparency = 0 Trapdoor.CanCollide = true ClickDetector.MaxActivationDistance = 10 COOLDOWN = false wait() end end) BookSwitch.XPart.Touched:Connect(function() if COOLDOWN == false then COOLDOWN = true Sound:play() Trapdoor.Transparency = 0.8 Trapdoor.CanCollide = false ClickDetector.MaxActivationDistance = 0 wait(COOLDOWN_TIME) Trapdoor.Transparency = 0 Trapdoor.CanCollide = true ClickDetector.MaxActivationDistance = 10 COOLDOWN = false Sound.Ended(wait) end end)