0
votes
local sounds = {
    877986525;
    2734549871;
}

local PrimaryQueue   = {} -- Player Chosen Songs. 
local SoundObj = workspace.MusicSystem
function PlaySoundAndWait(SoundId)
    SoundObj.SoundId = "rbxassetid://"..SoundId
    print("Loading Sound...")
    repeat wait() until SoundObj.IsLoaded
    print("Loaded")
    SoundObj:Play()
    repeat wait() until not SoundObj.Playing -- Wait till over.
end
local PlaySecondary = sounds
while wait(0.1) do
    if #PrimaryQueue ~= 0 then
        print("Play primary disregard current")
        -- Play primary, disregard current.
        PlaySoundAndWait(PrimaryQueue[1])
        table.remove(PrimaryQueue,1) -- Remove from queue (played)
    else
        -- Refill Secondary Queue if empty.
        if #PlaySecondary == 0 then
            print("REFILL")
            PlaySecondary = sounds
            print(#sounds)
            continue
        end
        print(PlaySecondary[1])
        PlaySoundAndWait(PlaySecondary[1])
        table.remove(PlaySecondary,1)
    end
end

When I refer to "REFILL" I mean line 26 where the list is refreshed. This script indefinitely checks if there's anything in the PrimaryQueue, if there is it plays that then removes it. If there's not it checks if the SecondaryQueue is empty, if so it refills it with "sounds". If it's not it plays the first sound then removes it. As a result all of this should create a music system, but for some reason when refilling, the sound list reads as empty. Even though it shouldn't be and has only been assigned a value once.

Thank you.

1

1 Answers

3
votes

You are doing table.remove(PlaySecondary, 1) which is equal to table.remove(sounds, 1) as they both point to the same table due to PlaySecondary = sounds, so it's coming back empty because you yourself removed all its elements previously!

I assume you wanted to create a copy of the table:

PlaySecondary = {unpack(sounds)}