You would do
while wait()do
for Index = 1,#MusicQueue do
if #MusicQueue >= 1 then
if MusicQueue[index] then --checking if the element in the table is not nil.
if PlayDebounce == false then
PlayDebounce = true
Sound.SoundId = "rbxassetid://"..tostring(MusicQueue[Index][1])
Sound.PlaybackSpeed = tonumber(MusicQueue[Index][2])
Sound:Play()
repeat wait() until Sound.TimePosition == Sound.TimeLength
table.remove(MusicQueue,Index)
PlayDebounce = false
end
end
end
end
end
Tell me if that works.
Though, I thought I should mention that the code you wrote skips 1 song each time the iteration runs.
This happens because when you use table.remove() on a table, not only does it set the index element to nil, it also shifts the rest of the elements to the left.
If your first iteration is something like
index = 1
MusicQueue = {sound1,sound2,sound3,sound4}
print(MusicQueue[index])
table.remove(MusicQueue,index)
it will output sound1.
But, the second iteration will be
index = 2
MusicQueue = {sound2,sound3,sound4}
print(MusicQueue[index])
table.remove(MusicQueue,index)
which will output sound3, instead of sound2, because table.remove shifted the rest of the elements to the left.
To prevent this from happening, you should instead use MusicQueue[Index] = nil. It will not shift the elements.
Or, you could just change all the “Index” in your code, to 1, so it always uses the first index of the MusicQueue. table.remove() will shift your elements to the left each iteration so you don’t need to increment Index.
Edit: if you need the MusicQueue to reset after the table is nil, then do what @XdJackyboiiXd21 said, though you still need to add my if condition and fix the problem I mentioned.
Edit2: Actually, I found out that you will never get nil if you just fix that problem I was talking about.