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