如何在Roblox Lua中向DataStore添加表格?

目标:制作一个帽子库存系统,保存所购买的帽子并将其添加到个人库存中。

目前状态:我有一个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的库存系统,只不过只有服装。我希望能够调用玩家的数据存储,如果库存中包括该帽子,则在他们的库存中显示它,以允许他们把它戴在头上。如果有人能帮我,那就太棒了!

点赞
用户88888888
用户88888888

你不能在 :SetAsync() 中保存表格,但是可以在 :UpdateAsync() 中保存。所以如果你按照以下方式操作,它应该可以工作:

local clothing = {"帽子1""衬衫1""等等"}

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

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

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

2020-01-30 00:01:49