找不到保存数据表的方法

huangapple go评论63阅读模式
英文:

Can't find a way to save a table of data

问题

我一直在开发一款基于“Booga Booga”的生存游戏,但似乎无法找出如何加载玩家数据到游戏中。我尝试加载的数据保存在以下的for循环中:

module.SaveData = function (player, DT)
    local data_saved = {}
    
    local setData = player.inventory.Inv:GetChildren()
    for i, v in pairs(setData) do
        table.insert(data_saved, {[v] = {
            value = v.Value,
            name = v.Name
        }})
    end
    Data_Store:SetAsync(player.UserId, data_saved)
end

我已经尝试了多种方法来解决这个问题

  1. 我尝试使用HTTP服务进行加载
  2. 我已经尝试加载原始表格
  3. 我尝试使用全局数据存储

目前这是我加载数据的代码:

game.Players.PlayerAdded:Connect(function(plr)
    CF.PlayerInventorySetup(plr) -- 不相关
    p = plr -- 不相关
    local PD = require(game.ServerScriptService.DataHandler)
    local plrdata = PD.FetchData(plr)
    
    for i, v in pairs(plrdata) do -- 这是我遇到问题的地方
        if not plr.inventory.Inv:FindFirstChild(v) then
            local newint = Instance.new("NumberValue")
            newint.Name = v.name -- 输出显示:期望字符串,但是得到了nil
            newint.Value = v.value -- 
            newint.Parent = plr.inventory.Inv
        end
    }
end)

实际上,我不知道该怎么做。

英文:

I have been working on a survival game base on "Booga Booga" but i cant seem to find out how to load player data on the game. The data im trying to load is saved in the for loop that follows:

module.SaveData = function (player, DT)
	local data_saved = {}
	
	local setData = player.inventory.Inv:GetChildren()
	for i, v in pairs(setData) do
		table.insert(data_saved, {[v] = {
			value = v.Value,
			name = v.Name
		}})
	end
	Data_Store:SetAsync(player.UserId, data_saved)
end

I've done multiple things to attempt to solve this problem

  1. I've tried load with http service
  2. I've already attempted loading the raw table
  3. and I've tried to use a global data store instead

here is my code that loads the data as of now:

game.Players.PlayerAdded:Connect(function(plr)
	CF.PlayerInventorySetup(plr) -- not relavent
	p = plr -- not relavent
	local PD =require(game.ServerScriptService.DataHandler)
	local plrdata =PD.FetchData(plr)
	
	for i, v in pairs(plrdata) do -- this is what im having trouble with
		if not plr.inventory.Inv:FindFirstChild(v) then
			local newint = Instance.new("NumberValue")
			newint.Name = v.name -- the ouput says: string expected, got nil
			newint.Value = v.value -- 
			newint.Parent = plr.inventory.Inv
		end
	end
end)

I actually don't know wtf to do.

答案1

得分: 0

在谈论解决方案之前,让我们谈谈保存数据时发生了什么。假设,举个例子,您的库存是一个类似以下的 NumberValues 列表:

  • Fish(值为3)
  • Iron(值为10)
  • Grass(值为8)

您的 FetchData 函数期望保存的玩家数据格式如下:

{
    { value = 3, name = "Fish" },
    { value = 10, name = "Iron" },
    { value = 8, name = "Grass" },
}

然而,在 SaveData 函数中的循环结果如下:

{
    { Fish(NumberValue) = { value = 3, name = "Fish" }},
    { Iron(NumberValue) = { value = 10, name = "Iron" }},
    { Grass(NumberValue) = { value = 8, name = "Grass" }},
}

在循环的每一步中,您都将一个新的字典推入了 data_saved 表中。FetchData 函数期望该字典具有 "name" 和 "value" 键。但是,您将这些数据推入了一个更深的层级,因此这些键不存在于 FetchData 代码所期望的位置,从而引发错误。

因此,要在 SetData 函数中解决问题,请移除该表的外部层,只使用原始数据。

table.insert(data_saved, {
    value = v.Value,
    name = v.Name
})
英文:

Before we talk about a solution, let's talk about what's happening when you save the data. Let's say for example, your inventory is a list of NumberValues like this :

  • Fish (value of 3)
  • Iron (value of 10)
  • Grass (value of 8)

Your FetchData function expects that the saved player data is formatted like this :

{
    { value =  3, name = "Fish"  },
    { value = 10, name = "Iron"  },
    { value =  8, name = "Grass" },
}

However, the result of the loop in the SaveData function would be this :

{
    { Fish(NumberValue) = { value=3, name="Fish" }},
    { Iron(NumberValue) = { value=10, name="Iron" }},
    { Grass(NumberValue) = { value=8, name="Grass" }},
}

On every step of the loop, you are pushing a new dictionary into the data_saved table.
The FetchData function expects that dictionary to have keys for "name" and "value". But, you've pushed that data one layer deeper, so those keys don't exist where the FetchData code expects them to and it throws errors.

So to fix your issue in the SetData function, remove the outer layer of that table, and just use the raw data.

table.insert(data_saved, {
    value = v.Value,
    name = v.Name
})

huangapple
  • 本文由 发表于 2023年2月10日 10:44:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406448.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定