为什么我的列表会清空/失去内容?[ROBLOX]

local sounds = {
    877986525;
    2734549871;
}

local PrimaryQueue   = {} -- 玩家选择的歌曲。
local SoundObj = workspace.MusicSystem
function PlaySoundAndWait(SoundId)
    SoundObj.SoundId = "rbxassetid://"..SoundId
    print("加载音频...")
    repeat wait() until SoundObj.IsLoaded
    print("加载完成")
    SoundObj:Play()
    repeat wait() until not SoundObj.Playing -- 等待直到结束。
end
local PlaySecondary = sounds
while wait(0.1) do
    if #PrimaryQueue ~= 0 then
        print("播放主要歌曲,忽略当前")
        -- 播放主要歌曲,忽略当前。
        PlaySoundAndWait(PrimaryQueue[1])
        table.remove(PrimaryQueue,1) -- 从队列中移除(已播放)
    else
        -- 如果次要队列为空,则重新填充。
        if #PlaySecondary == 0 then
            print("重新填充")
            PlaySecondary = sounds
            print(#sounds)
            continue
        end
        print(PlaySecondary[1])
        PlaySoundAndWait(PlaySecondary[1])
        table.remove(PlaySecondary,1)
    end
end

当我提到“重新填充”时,我指的是第26行,即列表被刷新的地方。 这个脚本不断检查PrimaryQueue中是否有任何东西,如果有,就播放并删除它。如果没有,则检查SecondaryQueue是否为空,如果是,则以“sounds”重新填充它。如果不是,则播放第一个声音,然后将其删除。 结果所有这些应该创建一个音乐系统,但由于某种原因,当重新填充时,声音列表会变成空的。即使它不应该这样,并且只被赋值了一次。

谢谢。

点赞
用户1871033
用户1871033

你正在执行 table.remove(PlaySecondary, 1),这与 table.remove(sounds, 1) 相同,因为它们都指向同一个表,这是由于 PlaySecondary = sounds,所以它现在为空,因为你之前已经移除了所有的元素!

我猜你想要 创建一个表的 副本:

PlaySecondary = {unpack(sounds)}
2021-07-05 19:53:36