Hello all,
As someone who doesn't yet have much experience with scripting yet, I run into these problems often. This is a part of a TweenService script that allows the door open when the button next to such door is clicked.
script.Parent.MouseClick:connect(function() if open == false then script.Parent.Sound:Play() Open1:Play() Open2:Play() wait(15) open = true else script.Parent.Sound:Play() Close1:Play() Close2:Play() wait(15) open = false end end)
I figured using a wait command would be a good way to make a 15 second cooldown for both the sound the door makes AND the door opening, but it only seems to be "cooling down" the ability to open the door, but the sound that the door itself makes can be constantly spammed if the door button is pressed a bunch of times, even when the door's open/close cooldown is still active.
My question is, how can I create a cooldown for the sound when the button is click in this portion of the script while maintaining the cooldown for the door itself to opened/closed?
Read your code from top to bottom, and try to interpret it as the computer would:
The problem is there's nothing actually stopping the sound from playing.
Assuming that Open1
, Open2
, Close1
and Close2
are tweens, I suggest the following script:
local open = false -- open starts being false local soundWait = false -- this variable will determine if the sound should play -- Set Open1, Open2, Close1 and Close2 here script.Parent.MouseClick:connect(function() -- First, play the appropiate tweens. The sound doesn't matter for these. if open then -- It's open right now, so close. Close1:Play() Close2:Play() open = false -- Set open to false so that next time it will open else -- It's not open right now, so open Open1:Play() Open2:Play() open = true -- Set open to true so that next time it will close end -- Lastly, play the audio if appropriate. if not soundWait then soundWait = true -- Prevent sound from playing during this wait script.Parent.Sound:Play() wait(15) soundWait = false -- Allow it to play again end end)
Fun fact: Preventing something from happening again during a set period of time is called a debounce.