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

Why does this play for 5 seconds then play a new song?

Asked by 7 years ago
Edited 7 years ago

This should select an id from the table and play it (other part in a local script), but instead the song plays for like ~5 seconds and then another song is played, and it keeps repeating itself.

It never waits for an actual song to finish before playing a new one.

work.SoundBrick.Sound.Ended:connect(function()
            if not debounce then
            debounce = true
            -- added to debug
            work.SoundBrick.Sound:Stop()

            local randomSong = songs[math.random(1, #songs)]
            print(randomSong)
            wait(0.5)
            remotes.Events.PlaySong:FireAllClients(randomSong) -- add in params
            wait(4)
            debounce = false
            end
        end)

2 answers

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

Get the length of the song (TimeLengh property) and put a wait() after the song is told to be played. The song should play fully.

0
Yeah this is the correct answer. I don't know why he didn't try that. Use the roblox wiki for help for problems like this. http://wiki.roblox.com/index.php?title=API:Class/Sound Whenever I have a problem I check the wiki first. If you know Lua's syntax properly, the wiki should help for most problems like this. legoguy939 418 — 7y
Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

Why it plays for 4 seconds, or 5, as you say, is because your debounce is set to 4. Meaning, after 4 seconds your script will re-loop and play a new song. Try to pick songs that are full length, which is 2 minutes, converted to 120 seconds. So for your wait, put wait(120), and it should work properly. This should be your final code:

work.SoundBrick.Sound.Ended:connect(function()
            if not debounce then
            debounce = true
            -- added to debug
            work.SoundBrick.Sound:Stop()

            local randomSong = songs[math.random(1, #songs)]
            print(randomSong)
            wait(0.5)
            remotes.Events.PlaySong:FireAllClients(randomSong) -- add in params
            wait(119.5) -- i did 199.5 because you have a .5 above this wait so this would make it exactly 120 seconds, which is 2 minutes, a full length roblox audio
            debounce = false
            end
        end)

Make sure to accept my answer if this helped.

Answer this question