For loop not working?Roblox studio

代码:

local DataStoreService = game:GetService("DataStoreService")
local InvDataStore = DataStoreService:GetDataStore("InvDataStore")

game.Players.PlayerAdded:Connect(function(player)
    local Id = player.UserId

    local Inventory = Instance.new("Folder")
    Inventory.Name = "Inventory"
    Inventory.Parent = player

    local Inv = InvDataStore:GetAsync(Id)
    print(Inv)
    print(table.concat(Inv, " "))
end)

game.Players.PlayerRemoving:Connect(function(player)
    local Id = player.UserId
    local InvTable = {}

    for i, v in pairs(game.Players:FindFirstChild(player.Name).Inventory:GetChildren()) do
        print("Repear")
        if v:IsA("NumberValue") then
            table.insert(InvTable, v)
            print(v)
        end

    end

    print(InvTable)
    print(table.concat(InvTable, " "))
    InvDataStore:SetAsync(Id, InvTable)
end)

输出:

13:25:35.288 - 未命名游戏自动恢复文件已创建。 现实主义模组当前运行v2.09!(×2) 表:0x08cb53598b2d3aa1

表:0xd8ce847b521d4091 1 13:26:26.703 - 断开连接从::ffff:127.0.0.1|60556

浏览器:

它似乎跳过了这个循环:

for i, v in pairs(game.Players:FindFirstChild(player.Name).Inventory:GetChildren()) do
        print("Repear")
        if v:IsA("NumberValue") then
            table.insert(InvTable, v)
            print(v)
        end

    end

好像不会打印重复(repeat)或值(Value)了解情况的人知道问题出在哪里吗?

注:我不明白的是,在保存之前和之后以及遗忘for循环之后,它不打印值。我可以提供额外的信息。

点赞
用户7572496
用户7572496

它忽略了循环,因为当它到达game.Players:FindFirstChild(player.Name)时,返回值将为nil,因为该玩家已经离开了服务器。你可以直接从你拥有的玩家对象中进行迭代,如果你已经拥有了它,就不需要寻找对象玩家。 尝试:

for i, v in pairs(player.Inventory:GetChildren()) do
    print("Repear")
    if v:IsA("NumberValue") then
        table.insert(InvTable, v)
        print(v)
    end
end

此外,良好的实践是在游戏期间存储数据,而不是在离开时存储数据,当玩家离开时,所有这些对象也会被移除,最好在游戏期间处理表格,在玩家移除期间仅更新数据存储。 良好的实践是每5分钟更新一次玩家的数据存储。

2020-10-25 02:15:15