How would I make my sound not spam when you click a gui

Hello I couldn’t get a good name for the title sorry

I was wondering where do I put a wait event in my script so the sound doesnt spam when you click on the gui.

what i mean is when you click the gui it plays the sound then when the sound is finished and your still clicking then it will play it again rather than just playing the first millisecond on a click

this is the script:

function PlaySound(play)

local sound = script.Parent.ClickSound -- Sound Name

sound:Play()

end

script.Parent.MouseButton1Click:Connect(PlaySound)

I’m kind of new to scripting so thanks if anyone can help out.

You can check if it’s playing or not using .IsPlaying, like such

function PlaySound(play)

local sound = script.Parent.ClickSound -- Sound Name

if not sound.IsPlaying then
  sound:Play()
end

end

script.Parent.MouseButton1Click:Connect(PlaySound)
6 Likes

Try using debounce.

local db = false

function PlaySound(play)

local sound = script.Parent.ClickSound -- Sound Name
    if not db then
        db = true
        sound:Play()
        task.wait(1)
        db = false
    end
end

script.Parent.MouseButton1Click:Connect(PlaySound)

^ You wont be able to play the sound until one second has passed, if you want the sound to be able to be played again only once it’s done, try using a .Ended function to detect if the song has ended.^

Do a combination between @indefiniteOptimist’s reply, and mine.

Here is some information on debounce: Debounce Patterns | Roblox Creator Documentation

5 Likes

ahhhhhh thats what it is ok thanks :DD

I thought i had to do like a wait() event haha thanks very much

1 Like

Modified your code a bit was bored lol.

local db = false

function PlaySound(play)

local sound = script.Parent.ClickSound -- Sound Name
    if not db then
        db = true
        sound:Play()
        sound.Ended:Wait()
        db = false
end
end

script.Parent.MouseButton1Click:Connect(PlaySound)
2 Likes