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

Why are the songs overlapping?

while true do
	Song = Instance.new("Sound")
	Song.Parent = workspace
	Song.Volume = 1
	Song.Looped = false
	Assets = {274504661,346191865,263966756,220964582,362105653}
	
	for _, asset in pairs(Assets) do
		game:GetService("ContentProvider"):Preload("http://www.roblox.com/asset/?id=" .. asset)
	end
	Song.SoundId = "http://www.roblox.com/asset/?id=" ..Assets[math.random(1, #Assets)]	
	Song:Play()

I want to make so it does 1 song at a time, and when 1 of the song stops, another one from the Assets table starts. Why is it constantly repeating ? Shouldn't the "while loop" wait for the Song to end?

1 comment

0

Sorry about not responding right away, I updated my answer, it might be a problem with the boolean not updating automatically? Anyhow, it should work now, I used one of the ideas Chemical posted in the comments. Be sure to give him an upvote if it works :P

1 answer

✓ Accepted answer

Simply put, no.

The loop doesn't wait for the song to end. Rather, it just continuously plays songs repeatedly, and seeing as how you have no waits in your script, it should actually crash the game via infinite loop (unless you have a wait at some other point in the "while loop").


First, we'll address the problem with the song.

I would suggest using a "repeat" to yield the loop. Here is an example of what I mean:

repeat wait() until Song.IsPlaying == false

This just loops over and over again until the song is done playing, then allows the code to pass this point. As an added bonus, you won't actually have to include any other waits than the one I included in this function (unless you choose to of course).


Here's what your entire while loop should look like with this added in:

while true do
    Song = Instance.new("Sound")
    Song.Parent = workspace
    Song.Volume = 1
    Song.Looped = false
    Assets = {274504661,346191865,263966756,220964582,362105653}

    for _, asset in pairs(Assets) do
        game:GetService("ContentProvider"):Preload("http://www.roblox.com/asset/?id=" .. asset)
    end
    Song.SoundId = "http://www.roblox.com/asset/?id=" ..Assets[math.random(1, #Assets)] 
    Song:Play()
    wait(Song.TimeLength)
end

Anyways, I hope this helped. If not, or if you have any further problems/questions, please leave a comment below, and I'll see what I can do.

3 comments

1

There are also other alternatives such as wait(Song.TimeLength) and the :Ended function of a sound.

0

Yup, thanks for pointing that out ^

0

It doesn't get passed " repeat wait() until Song.IsPlaying == false " whenever the song ends, it doesn't choose a new one. It stays at that line, I've tried putting a print in the next line, and it doesn't get passed that line.

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