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

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I am here because I would like to know how it is possible to prevent people from spamming a sound a button that opens a door makes.

  2. What is the issue? Include screenshots / videos if possible!
    I’m having trouble finding out how I can add a cooldown to a specific part of my TweenService script (which opens a heavy blast door.) When the button that opens the door is pressed, the door will open and a sound will play. I dont run into any problems until I realize that the door’s open sound will play if you try to click it again mid-animation or during its open/close cooldown.

Here is the part of the script I am trying to change.

script.Parent.MouseClick:connect(function()
	if open == false then
		Open1:Play()
		Open2:Play()
		script.Parent.Sound:Play() 
			wait(15)
		open = true
	else
		Close1:Play()
		Close2:Play()
		script.Parent.Sound:Play() 
			wait(15)
		open = false
	end
end)
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    So far, I have tried moving the “wait(15)” command to different parts of the script, to see if it would cool down the sound as well as the door open/close tween animations. I have heard of debouncing, but I don’t know how to insert a debounce in between this part of the script without ending up ruining something else.

What should I do?

It’s as simple as removing the else statement and just keeping it as if open isn’t equal to true, then set open to true and wait 15 seconds after setting it to true to false afterwards.

Add a debounce like this:

local cooldown = 0.5 --you can change this if you like
local canClick = true

script.Parent.MouseClick:connect(function()
    canClick = false
	if open == false then
		Open1:Play()
		Open2:Play()
		script.Parent.Sound:Play() 
			wait(15)
		open = true
	else
		Close1:Play()
		Close2:Play()
		script.Parent.Sound:Play() 
			wait(15)
		open = false
	end
    wait(15 + cooldown) -- we add the 15 to also wait for the door
    canClick = true
end)
1 Like

Thank you, this worked perfectly!

As @Bikereh said, when you want to prevent people from click spamming or something alike, thats where debounces arrive and can help you with this. Best of luck !
[Debounces are own values, that we make in order to prevent spamming and stuff like that, you can use numbers, boolean, etc…, most common one is boolean tho]