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

Click Detector Sound Play Broken?

Asked by
ImfaoXD 158
7 years ago

Can somebody help me? When I click on the "Part" the song play and while the song is playing, I tried to click it again and the song start all over again. I don't want that, I want the song to play until it is finish and then I can click it again to play the song. I already added in the code for it but for some reason it doesn't work in the actual game when I tested it. It work in studio though and I can't find the solution to it. Any idea why is this happening?

Brick = script.Parent
Sound = Brick.Sound
Debounce = false

function onClicked()
    if not Debounce then -- If the debounce is false
        Debounce = true -- Change it to true
        Sound:Play()
        wait(Sound.TimeLength) -- Wait til it ends
        Debounce = false -- Change it to false
    end -- End the if function
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)
0
Your script is working for me, can you show the hierarchy? User#5423 17 — 7y
0
Did you tested it in studio or the game? ImfaoXD 158 — 7y
0
Here's the radio layout I've made: https://gyazo.com/98b9cc2e1c0c05b70dc519916141df36 ImfaoXD 158 — 7y
0
Sorry about my previous answer not working, I've updated it now! Hopefully that will work for you. I didn't realise .Loaded and .IsLoaded were disabled. General_Scripter 425 — 7y

1 answer

Log in to vote
2
Answered by 7 years ago
Edited 7 years ago

Your problem is that when sounds haven't loaded, the Sound.TimeLength is automatically 0. You'll need to wait for the song to load, this can be done by repeating wait() until the sound length is more than 0. Here's what your script should be:

Brick = script.Parent
Sound = Brick.Sound
Debounce = false

function onClicked()
    if not Debounce then -- If the debounce is false
        Debounce = true -- Change it to true
        repeat wait() until Sound.TimeLength > 0 or Sound.SoundId == 0
        Sound:Play()
        wait(Sound.TimeLength)
        Debounce = false
        Sound:Stop()
    end
end

Brick.ClickDetector.MouseClick:connect(onClicked)
0
Thank you so much but there's is a small problem though, after the song done playing. I can't make it play again. I click it and the song is not playing. Please help... ImfaoXD 158 — 7y
0
It's completely broken. I click it and nothing happen, no song. What's wrong with it?! D; ImfaoXD 158 — 7y
Ad

Answer this question