Why does the next song not play?

I’m new to coding please help.
It plays the first song and then stops unless it’s looped.

local id = {"5410085694","5410086218","5410081542"}

local chosenid = id[math.random(1, #id)]

while true do
wait(1)
	local m = Instance.new("Sound",Workspace)
m.Looped = true
m.Name = "GamePlayAudio1"
m.SoundId = "rbxassetid://"..chosenid
m.Volume = 2
m:Play()
wait(5)
if m.Playing == true then
	repeat
		wait()
	until m.Playing == false
end
return
end

Hey there, one thing I noticed right off the bat is that you have “Workspace” capitalized when defining your new instance. You should correct it to either:

local m = Instance.new("Sound",workspace)

Or:

local m = Instance.new("Sound",game.Workspace)
1 Like

This is why the next song never plays. Also, I noticed this:

You’re selecting the song only once since it’s outside the while loop. Here’s some improved code(not tested):

local Songs = { "5410085694","5410086218","5410081542" }
local random = Random.new()

while task.wait(1) do
    local ChosenSong = Songs[random:NextInteger(1,#Songs)]

    local Sound = Instance.new("Sound")
    Sound.Parent = workspace
    Sound.Name = "GamePlayAudio1"
    Sound.SoundId = "rbxassetid://"..ChosenSong
    Sound.Volume = 2
    Sound:Play()
    Sound.Ended:Wait()
end

Would that be the entire code? I tested it and it is the same result, the song doesn’t continue playing or change.

Hm that’s strange…
I’m currently trying to come up with a solution I’ll be back when I fix the code

1 Like

Tried it again and it seemed to work, but thats Roblox for you I guess, but there’s a new audio every time a new song is picked. Is there a way to delete the old audio? Or use the same one? Audio as in the speaker.

Oh my bad I forgot to delete the old audio

Put this under Sound.Ended:Wait()

Sound:Destroy()

Or if you want to re-use the same audio:

local Songs = { "5410085694","5410086218","5410081542" }
local random = Random.new()
local Sound = [PATH_TO_SOUND]

while task.wait(1) do
	local ChosenSong = Songs[random:NextInteger(1,#Songs)]
	Sound.SoundId = ChosenSong
	Sound:Play()
	Sound.Ended:Wait();Sound.SoundId = ""
end
2 Likes
local Randoms = Random.new()

local Sound = Instance.new("Sound")
Sound.Name = "GamePlayAudio1"
Sound.Volume = 2
Sound.Parent = workspace

local Sounds = { "5410085694","5410086218","5410081542" }

while true do
	local RandomSound = Sounds[Randoms:NextInteger(1, #Sounds)]
	Sound.SoundId = "rbxassetid://"..RandomSound
	if not Sound.IsLoaded then
		Sound.Loaded:Wait()
	end
	Sound:Play()
	Sound.Ended:Wait()
end

It would be more efficient to just instance a single “Sound” instance and use that instead. You should probably wait for the “Sound” instance to load each time its “SoundId” property is changed before calling the instance method “:Play()” on it.

2 Likes