Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How can I prevent the sound thats played from being spammed on click?

Asked by 2 years ago

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?

0
try indenting the wait(15) or putting it in the same line as the sound:Play() realTongoKong 7 — 2y
0
"try indenting the wait(15) or putting it in the same line as the sound:Play()" i've tried this, but instead of making the sound have a cooldown it simply make the entire script wait 15 seconds before the door opens and the sound plays... i'll try intenting but it might not work kingsworld9 14 — 2y

1 answer

Log in to vote
0
Answered by
Aimarekin 345 Moderation Voter
2 years ago

Read your code from top to bottom, and try to interpret it as the computer would:

  1. User clicks. Event fires.
  2. Open is not true. Condition is met.
  3. Sounds and other objects are played.
  4. 15 second wait.
  5. During this 15 second wait, the user clicks again.
  6. Open is not true. Condition is met.
  7. !!! Sounds and other objects are played !!!

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.

Ad

Answer this question