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)
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)