Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I make it so that this sound only plays once?

I made a script that makes it so that when a player clicks on my selected model, it plays a sound How can i make it so that after a player clicks on the part once it does not play again when clicking on it a second time?

This is the script I'm currently using:

Brick = script.Parent
Sound = Brick.Sound2

function onClicked()
Sound:Play()
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

2 answers

✓ Accepted answer

Change :connect to :Connect in your last line.

local brick = script.Parent
local sound = brick.Sound2

local clicks = 0

function onClick(plr)
    clicks = clicks + 1 
    sound:Play()
    if clicks == 1 then
        sound.Parent = nil -- didn’t use :Destroy() just in case you wanted it to be re-parented
    end
end

3 comments

0

This shouldn't be an answer and should be a comment instead.

0

Nevermind. I didn't see your also fixing the issue. I thought you were just saying to use Connect().

0

Thanks so much :)

Make a debounce variable, and also no need to check the counter, as that might be slightly more complicated:

local brick = script.Parent
local sound = brick.Sound2

local debounce = false

function onClick(plr)
    if debounce then return end
    debounce = true -- This will make it not run again.
    clicks = clicks + 1 
    sound:Play()
    sound.Parent = nil
end

3 comments

0

Oh, and also remove the clicks variable. That won't work as that is no longer declared... sorry for my mistake!

0

thanks :)

0

You are welcome.

This discussion is preserved as it was. New answers, comments, and votes are closed.