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

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

Asked by
yayachik1 -11
5 years ago
Edited 5 years ago

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

Log in to vote
0
Answered by 5 years ago

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
0
This shouldn't be an answer and should be a comment instead. Dog2puppy 168 — 5y
0
Nevermind. I didn't see your also fixing the issue. I thought you were just saying to use Connect(). Dog2puppy 168 — 5y
0
Thanks so much :) yayachik1 -11 — 5y
Ad
Log in to vote
-1
Answered by 5 years ago

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
0
Oh, and also remove the ``clicks`` variable. That won't work as that is no longer declared... sorry for my mistake! Pixelated_MC 33 — 5y
0
thanks :) yayachik1 -11 — 5y
0
You are welcome. Pixelated_MC 33 — 5y

Answer this question