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

Why won't my while music loop,loop again?

I'm trying to make a script that plays a random song from a folder i made in the sound service.

local sound = true
local CurrentSound = nil



while sound do
local Music = game:GetService("SoundService").MusicFolder:GetChildren() -- gets all of my sounds and puts them in a folder
local song = Music[math.random(1,#Music)] -- picks a random Song
song:Play() -- plays the sound
song.Ended:Wait(2) -- waits until the sound has ended and then it will wait for 2 seconds
-- should loop again and pick a random number and play it again but it doesn't?
end

It works when i play the game but after a song has played it just stops and doesn't play any more music just the first song it chooses

2 comments

0

Try calling :Stop() or clearing the variable Song.

0

YO i fixed it i still accepted the answer though because it helped me fix it the problem was that it was in sound service and sound doesn't play in the sound service so i just put it in workspace

1 answer

✓ Accepted answer

The reason is that it only affects the while loop try adding a coroutine.

local SoundService = game:GetService("SoundService")
local MusicFolder = SoundService:WaitForChild("MusicFolder")

local Sound = true
local CurrentSound;

while (Sound) do
	for _,Song in pairs(MusicFolder:GetChildren()) do
		if (Song:IsA("Sound")) then
			CurrentSong = Song
			CurrentSong:Play()
			Song.Ended:Wait(2)
			CurrentSong:Stop()
		end
	end
end

Reorganized by Feahren.

25 comments

0

Your IIFE is malformed, you’ll also cause a massive overlap problem.

0

it still doesn't work also what is a coroutine?

0

That invokes the topic of Threading. Let’s view a program as a single strand of thread, running in one straight line, a descending task. Our code will sometimes yield (hold) and prevent us from transcending further down the thread. Why is this an issue? The instructions below won’t be ran until the instructions above are completed.

0

Sometimes, this can be an issue, as we need code to compile whilst other is running, so how to we run two sections of code simultaneously in one script? We essentially split our single thread into two; this will give us the capability to run a process while the program continues.

0

TL;DR: It mimics the action of creating another script to work in parallel with another, executing two instructions without interrupting any flow.

0

Coroutines provide more flexibility in this field, though aren’t really necessary in situations like such as we can use a specialized function called spawn() which directly runs the scope on another thread with simplicity without the complexity of having to take up space with a coroutine. I suggest you look up “Threading” in generic Computing Science to get a better idea, then looking into ROBLOX’

0

oh so Coroutines allow something like two code blocks to run at the same time without waiting for one to finish

0

Basically.

0

The reason why it will cause an overlap problem, is that it embeds the .Ended signal yield within a separate thread, which means it won’t effect the while loop and the loop will continue on. Since none if the previous Sound Objects are removed, or stopped, it will keep picking sounds and playing them which end’s with a tedious cesspool of noise to deal with.

0

He is using an Immediately Invoked Function Expression (IIFE) to call upon the routine returned by the .wrap function, which is normal if we wish to use a separated thread quickly. It’s malformed as the callback parentheses aren’t matched properly, so the compiler will have a difficult time, as the arguments aren’t closed, and a while loop is being matched with a ).

0

Basically a mess.

0

ok

1

Feahren edit the code then or answer it then

0

So are you saying that after the wait() i need to stop the music from playing so i won't end up with a lot of music overlapping each other

0

well i got a understanding of coroutines i'll just go watch a tutorial on it

1

I edited it this might work s

0

ok i think im understanding the thread thing so like if i have a if statement with elseif's in it,It would read all of it then it executes it.But is this how it works because this is my understanding of it

0

I rewrote it, though, the overlap issue still remains, using a thread is what will cause this.

0

it exhausts the execution time in the error i'm not sure why though

0

maybe using a in pairs loop like for i,v in pairs(Music) if v:IsPlaying and i == 4 then v:Stop() but i'm not sure how to make i return the number of sounds playing something like that

0

I can do that for you, I also said, It will ignore the yield since it's in a separate thread, thus causing the loop to have no rest.

0

Try giving that a go.

0

i tried it and i just redid the whole script

0

Ok im very close to solving this now so i made a corantine and it just picks a random number for me so know i gatta figure out how to loop the music with that number

0

well i just don't know why it isn't looping after the CurrentSound:Stop() it just doesn't play any song anymore after it stops

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