Music system not playing next song

I made a music system script, but it doesnt play the next random song, it just only plays that song

local ids = { , , , ,}





	local idss = ids[math.random(1, #ids)]
script.Parent.Sound.SoundId = "rbxassetid://"..idss
script.Parent.Sound:Play()
while wait() do
	script.Parent.Sound.Ended:Connect(function()
		script.Parent.Sound.Looped = false
	script.Parent.Sound.SoundId = "rbxassetid://"..idss
	script.Parent.Sound:Play()
end)

end
2 Likes

Change that to this as one: You making a connection every second, Two: Don’t use wait(), use task.wait() as task.wait is faster.

Last of all, make use the idss is in the loop, not the whole script otherwise it will play the same song.

Fill the ids table with ids(ie, {1,2,3,4})

while wait() do
	local idss = ids[math.random(1, #ids)]
	script.Parent.Sound.Ended:Wait()
	script.Parent.Sound.Looped = false
	script.Parent.Sound.SoundId = "rbxassetid://"..idss
	script.Parent.Sound:Play()
end

You seem to be picking a song outside of the loop, meaning you only pick the song once. I recommend something similar to this:

local ids = {, , , , } -- your sound ids here

local sound = script.Parent.Sound -- reference to sound instance

while task.wait() do
	for index,id in ipairs(ids) do -- loop through all of the songs
		sound.SoundId = "rbxassetid://"..id
		sound:Play()
		sound.Ended:Wait() -- wait for the song to end
	end
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.