Sound.Ended not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want the list to be mixed up and then played, and once that whole list is played then the list is mixed up again and played again. This is so the songs don’t keep playing in the same order.
  2. What is the issue? Include screenshots / videos if possible!
    The script isn’t working for some reason and I cant figure out what it is. I know its not working by the fact its not printing “yes” I do know the first part is working though because the first song plays but nothing after that.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Changing the order of the script because I thought that was the issue
local Sounds = {
	9046865270,
	9047105533,
	9046863253,
	9047104919,
	9046863579,
	9047105702,
	9046864489,
	9046863235,
	9047104336,
	9047104571,	
}
local SoundInstance = Instance.new("Sound", game.SoundService)

while true do
	local Table = {}
	local SoundsRemaining = #Sounds
	repeat 
		local Sound
		repeat
			Sound = table.insert(Table, Sounds[math.random(1, #Sounds)])
		until table.find(Table, Sounds[Sound]) == nil
		table.insert(Table, Sound)
		SoundsRemaining = SoundsRemaining - 1
	until SoundsRemaining == 0
	local currentSound = 0
	SoundInstance.SoundId = "rbxassetid://"..Table[1]
	currentSound = 1
	local Connection = SoundInstance.Ended:Connect(function()
		print("yes")
		if currentSound >= #Table + 1 then

		else
			SoundInstance.SoundId = "rbxassetid://"..Table[currentSound + 1]
			currentSound = currentSound + 1
			SoundInstance:Play()
		end
	end)
	SoundInstance:Play()
	print(Connection.Connected)
	Connection:Disconnect()
	repeat task.wait(1) until currentSound >= #Table + 1
end```
2 Likes

You could maybe use a for loop instead of a repeat

2 Likes

Will be a more pain to write.

Maybe try playing the audio before the connection is set?

2 Likes

Try this:

local Sounds = {
	9046865270,
	9047105533,
	9046863253,
	9047104919,
	9046863579,
	9047105702,
	9046864489,
	9046863235,
	9047104336,
	9047104571,	
}
local SoundInstance = Instance.new("Sound", game.SoundService)

function shuffle(tbl)
  for i = #tbl, 2, -1 do
    local j = math.random(i)
    tbl[i], tbl[j] = tbl[j], tbl[i]
  end
  return tbl
end

while true do
	local shuffledSounds = shuffle(Sounds)
	local currentSound = 1
	SoundInstance.SoundId = "rbxassetid://"..shuffledSounds[currentSound]
	local Connection = SoundInstance.Ended:Connect(function()
		currentSound = currentSound + 1
		if currentSound > #shuffledSounds then
			Connection:Disconnect()
		else
			SoundInstance.SoundId = "rbxassetid://"..shuffledSounds[currentSound]
			SoundInstance:Play()
		end
	end)
	SoundInstance:Play()
	repeat task.wait(1) until not Connection.Connected
end
1 Like

Why don’t you use Sound.Ended:Wait() ?

1 Like

You’re instantly killing the connection before it even has a chance to fire by having Connection:Disconnect() come before the last repeat until loop.

I ended up rewriting the entire thing while I was ironing out some other issues with your code, so here’s a new and improved script LOL. Enjoy.

local Sounds = {
	9046865270,
	9047105533,
	9046863253,
	9047104919,
	9046863579,
	9047105702,
	9046864489,
	9046863235,
	9047104336,
	9047104571,
}

local SoundInstance = Instance.new("Sound", game.SoundService)
local LastSoundID = nil

while true do
	local ShuffledList = {}
	repeat
		local Index = math.random(1,#Sounds)
		table.insert(ShuffledList,Sounds[Index])
		table.remove(Sounds,Index)
	until #Sounds == 0
	if ShuffledList[1] == LastSoundID then --// Prevent the same sound from being played twice in a row by moving it to be back of the playlist
		table.insert(ShuffledList,ShuffledList[1])
		table.remove(ShuffledList,1)
	end
	LastSoundID = ShuffledList[#ShuffledList]
	for i,v in pairs(ShuffledList) do
		SoundInstance.SoundId = "rbxassetid://"..v
		SoundInstance:Play()
		task.wait(SoundInstance.TimeLength + 1)
	end
	Sounds = ShuffledList
end
1 Like

Thanks for the help, I ended up realizing I did end up killing the connection before it could even run :man_facepalming:t3:

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