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

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

Asked by 4 years ago

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

0
Try calling :Stop() or clearing the variable Song. Ziffixture 6913 — 4y
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 thebananashetoldyou 31 — 4y

1 answer

Log in to vote
1
Answered by
JesseSong 3916 Moderation Voter Community Moderator
4 years ago
Edited by Ziffixture 4 years ago

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.

0
Your IIFE is malformed, you’ll also cause a massive overlap problem. Ziffixture 6913 — 4y
0
it still doesn't work also what is a coroutine? thebananashetoldyou 31 — 4y
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. Ziffixture 6913 — 4y
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. Ziffixture 6913 — 4y
View all comments (21 more)
0
TL;DR: It mimics the action of creating another script to work in parallel with another, executing two instructions without interrupting any flow. Ziffixture 6913 — 4y
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’ Ziffixture 6913 — 4y
0
oh so Coroutines allow something like two code blocks to run at the same time without waiting for one to finish thebananashetoldyou 31 — 4y
0
Basically. Ziffixture 6913 — 4y
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. Ziffixture 6913 — 4y
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 ). Ziffixture 6913 — 4y
0
Basically a mess. Ziffixture 6913 — 4y
0
ok thebananashetoldyou 31 — 4y
1
Feahren edit the code then or answer it then JesseSong 3916 — 4y
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 thebananashetoldyou 31 — 4y
0
well i got a understanding of coroutines i'll just go watch a tutorial on it thebananashetoldyou 31 — 4y
1
I edited it this might work s JesseSong 3916 — 4y
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 thebananashetoldyou 31 — 4y
0
I rewrote it, though, the overlap issue still remains, using a thread is what will cause this. Ziffixture 6913 — 4y
0
it exhausts the execution time in the error i'm not sure why though thebananashetoldyou 31 — 4y
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 thebananashetoldyou 31 — 4y
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. Ziffixture 6913 — 4y
0
Try giving that a go. Ziffixture 6913 — 4y
0
i tried it and i just redid the whole script thebananashetoldyou 31 — 4y
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 thebananashetoldyou 31 — 4y
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 thebananashetoldyou 31 — 4y
Ad

Answer this question