Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make my door only open once on Touched and play sound only once?

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)

1 answer

✓ Accepted answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.