如何将 Table 添加到 Roblox Lua 中的 DataStore?

How do I add a Table to a DataStore in Roblox Lua?

目标:为帽子制作一个库存系统,这样当您购买一顶帽子时,它会将它添加到您的库存中。

我目前拥有的:现在我有一个 IntValue 在他们加入时添加到玩家(不是角色)。这个 IntValue 被命名为 "CurrentHat" 并且被设置为玩家上次保存的他们戴的帽子的值。在此之后,它会等到角色加载时使用 CurrentHat 的值通过从 ServerStorage 获取帽子来将其添加到玩家头部。然后,如果 CurrentHat 的值发生变化,它会连接它以添加玩家帽子功能并将其连接到数据存储区。下面是将数据添加到游戏中的代码部分,以及我认为应该将库存数据添加到游戏中的部分。所有注释掉的东西都是我已经尝试过的(失败了)。

function playeradded(player)
    print("hello")
    player:LoadCharacter()
    local leaderstats = Instance.new("IntValue")
    leaderstats.Name = "leaderstats"
    local hat = Instance.new("StringValue")
    hat.Name = ("CurrentHat")
    local coins = Instance.new("IntValue")
    coins.Name = "Coins"
    coins.Parent = leaderstats
    leaderstats.Parent = player
    hat.Parent = player
    hat.Value = ds2:GetAsync(player.UserId) or math.floor(math.random(0,1))
    --table.insert(hattable, hat.Value)
--  ds3:SetAsync(player.UserId,hat.Value)
    --for index,value in pairs (hattable) do
   -- ds3:UpdateAsync(index, function() return value end)
    --end
    --print(hattable[1])
    --print(ds3)
    local playerHat = hat.value
    hat.Changed:connect(function()
    ds2:SetAsync(player.UserId,hat.Value)
    --ds3:SetAsync(player.UserId,table.insert(hat.Value))
    --print(ds3)
    end)
    coins.Value = ds1:GetAsync(player.UserId) or 0
    ds1:SetAsync(player.UserId,coins.Value)

我想做的一个很好的例子是流行的 Roblox 游戏 SwordBurst 的库存系统,除了只有衣服。

我希望能够调用玩家数据存储,如果帽子包含在数据存储中,则将其显示在他们的库存中以允许他们戴上它。如果有人能帮助我,那就太棒了!

您不能在 :SetAsync() 中保存表格,但可以在 :UpdateAsync() 中保存表格,因此如果您只执行以下操作,它应该可以工作:

local clothing = {"Hat1","Shirt1","etc"}

local datastore = game:GetService("DataStoreService"):GetDataStore("Clothing")

datastore:UpdateAsync("A_key",function()
    return clothing
end)

这些变量只是示例。请相应地更改一些内容