Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What's wrong with this code? (Music system)

Asked by 2 years ago
Edited 2 years ago
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.

Answer this question