【问题标题】:How do I add a Table to a DataStore in Roblox Lua?如何在 Roblox Lua 中将表添加到 DataStore?
【发布时间】:2019-11-22 17:08:55
【问题描述】:

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

我目前拥有的:现在我有一个 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 的库存系统,除了只有衣服。

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

【问题讨论】:

    标签: lua roblox


    【解决方案1】:

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

    local clothing = {"Hat1","Shirt1","etc"}
    
    local datastore = game:GetService("DataStoreService"):GetDataStore("Clothing")
    
    datastore:UpdateAsync("A_key",function()
        return clothing
    end)
    

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

    【讨论】:

      猜你喜欢
      • 2020-10-28
      • 2021-01-26
      • 2015-03-09
      • 2021-12-16
      • 2021-04-16
      • 2015-01-07
      • 2021-04-14
      • 2021-07-31
      • 2021-11-06
      相关资源
      最近更新 更多