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

Click Detector Sound Play Broken?

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)

4 comments

0

Your script is working for me, can you show the hierarchy?

0

Did you tested it in studio or the game?

0

Here's the radio layout I've made: https://gyazo.com/98b9cc2e1c0c05b70dc519916141df36

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.

1 answer

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)

2 comments

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

0

It's completely broken. I click it and nothing happen, no song. What's wrong with it?! D;

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