如何在Roblox Lua的数据存储中添加表?

时间:2019-07-13 00:40:02

标签: lua roblox

目标:为帽子制造一个可以节省的库存系统,因此,当您购买帽子时,会将其添加到库存中。

我目前拥有的东西:现在我有一个IntValue,当他们加入时,它会添加到玩家(不是角色)中。此IntValue名为“ CurrentHat”,并设置为玩家上次保存的帽子所戴的值。此后,它一直等待到角色加载为止,并通过从ServerStorage获取使用CurrentHat的值将该帽子添加到玩家的头部。然后,如果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的库存系统。

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

1 个答案:

答案 0 :(得分:0)

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

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

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

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

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

相关问题