How to stop sound from playing multiple times when clicked?

Hello, I am currently working on a Light Switch that plays a sound when you click it.

cd.MouseClick:Connect(function()
	if clicked == false then
		tweenserv:Create(switch, TweenInfo.new(0.5, Enum.EasingStyle.Quad), {Orientation = On.Orientation}):Play()
		
		wait(0.5)
		switch:WaitForChild("OnSound"):Play()
		wait(2)
		clicked = true
		
		else 
		tweenserv:Create(switch, TweenInfo.new(0.5, Enum.EasingStyle.Quad), {Orientation = Off.Orientation}):Play()
		
		wait(0.5)
		switch:WaitForChild("OffSound"):Play()
		wait(2)
		clicked = false
	
	end
end)

As you can see the script waits 2 seconds before it allows you to switch the light back on or off and that works perfectly but the issue is that when you repeatedly click the switch the sounds are still going to play either way and i don’t know how to fix that. I only want the sounds to play once when the switch is clicked on or off. Thank you for your time.

1 Like

Move the click sound play underneath clicked = true and clicked = false

Actually, after if clicked == false right after that put clicked = true then 2 seconds later clicked = false

Do the same for the reverse conditional check.

Sorry I’m on mobile it’s not letting me just edit the code properly myself

Thank you for replying. I did try that but unfortunately when i spam the button the sound still plays more than once and doesnt wait for the switch to actually change from on to off

1 Like

After the click, if clicked == false then clicked = true tween script wait 2 clicked = false

1 Like

I did try that. Didn’t work though. );

If I’m being honest, I do know how to fix your script, what you need to do is wrap the code in a debounce check, then use a logic for detecting if the light is on or off, then deciding how to change the light’s status. But I’m on my phone and the code isn’t pasting right so my bad. Someone will solve it soon

Debounces need to have an option to do nothing, but both logic conditions have some code to run.
Try this format:

local tweens = {
    On = tweenserv:Create(switch, TweenInfo.new(0.5, Enum.EasingStyle.Quad), {Orientation = On.Orientation}),
    Off = tweenserv:Create(switch, TweenInfo.new(0.5, Enum.EasingStyle.Quad), {Orientation = Off.Orientation})
}
local state = "Off"

cd.MouseClick:Connect(function()
	if clicked == false then
                clicked = true
                if state == "On" then
                     state = "Off"
                else
                     state = "On"
                end
		tweens[state]:Play()
		
		wait(0.5)
		switch:WaitForChild(state.."Sound"):Play()
		wait(2)
		clicked = false
	
	end
end)
2 Likes

for some reason im getting an error in the table you wrote. The “Off” statement is showing up red. Also it doesn’t work and i don’t really understand why

I may have missed a , in the table, whoops.

Thank you very much! It works perfectly.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.