字典求助/DataStore
2019-11-5 20:28:5
收藏:0
阅读:72
评论:1
问题是我有一个包含所有数据的字典,它应该能够将其转换为在复制存储中的目录,所有值都是字符串,然后当玩家离开时将所有键转换回字典。但是,我无法想出如何将它转换为带有键的字典。
我已经测试了几个小时,但在第一层值之后,我无法想出一种将更深层次的值和键转入表格的方法。
local DataTable =
{
["DontSave_Values"] =
{
["Stamina"] = 100;
};
["DontSave_Debounces"] =
{
};
["TestData"] = 1;
["Ship"] =
{
["Hull"] = "Large_Ship";
["Mast"] = "Iron_Tall";
["Crew"] =
{
["Joe One"] =
{
["Shirt"] = "Blue";
["Pants"] = "Green"
};
["Joe Two"] =
{
["Shirt"] = "Silver";
["Pants"] = "Brown";
["Kids"] =
{
["Joe Mama1"] =
{
["Age"] = 5
};
["Joe Mama2"]=
{
["Age"] = 6
};
}
};
}
};
["Level"] =
{
};
["Exp"] =
{
};
}
------测试它是否为数组
function isArray(Variable)
local Test = pcall(function()
local VarBreak = (Variable.." ")
end)
if Test == false then
return true
else
return false
end
end
------转换为文件夹
function CreateGameDirectory(Player, Data)
local mainFolder = Instance.new("Folder")
mainFolder.Parent = game.ReplicatedStorage
mainFolder.Name = Player.UserId
local function IterateDictionary(Array, mainFolder)
local CurrentDirectory = mainFolder
for i,v in pairs(Array) do
if isArray(v) then
CurrentDirectory = Instance.new("Folder", mainFolder)
CurrentDirectory.Name = i
for o,p in pairs(v) do
if isArray(p) then
local TemporaryDir = Instance.new("Folder", CurrentDirectory)
TemporaryDir.Name = o
IterateDictionary(p, TemporaryDir)
else
local NewValue = Instance.new("StringValue", CurrentDirectory)
NewValue.Name = o
NewValue.Value = p
end
end
else
local value = Instance.new("StringValue", mainFolder)
value.Name = i
value.Value = v
end
end
end
IterateDictionary(Data, mainFolder)
end
------将其转换回表格
function CreateTable(Player)
local NewDataTable = {}
local Data = RS:FindFirstChild(Player.UserId)
local function DigDeep(newData, pData, ...)
local CurrentDir = newData
for i,v in pairs(pData:GetChildren()) do
if string.sub(v.Name,1,8) ~= "DontSave" then
end
end
end
DigDeep(NewDataTable, Data)
return NewDataTable
end
我希望当玩家离开时运行 createtable 函数,并将复制存储中的所有实例转换回带有键的字典。
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

如果其他人有这个问题,我用的解决方案是把实例直接转换成 JSON 格式(我用名称作为键)。这样我就可以保存它,然后当玩家重新加入时,我只需使用 JSONDecode 将其转换为我所需的字典。
function DirToJSON(Player) local NewData = RS:FindFirstChild(Player.UserId) local JSONstring="{" local function Recurse(Data) for i, v in pairs(Data:GetChildren()) do if v:IsA("Folder") then if #v:GetChildren() < 1 then if i == #Data:GetChildren()then JSONstring=JSONstring..'"'..v.Name..'":[]' else JSONstring=JSONstring..'"'..v.Name..'":[],' end else JSONstring=JSONstring..'"'..v.Name..'":{' Recurse(v) if i == #Data:GetChildren()then JSONstring=JSONstring..'}' else JSONstring=JSONstring..'},' end end else if i == #Data:GetChildren()then JSONstring=JSONstring..'"'..v.Name..'":"'..v.Value..'"' else JSONstring=JSONstring..'"'..v.Name..'":"'..v.Value..'",' end end end end Recurse(NewData) JSONstring = JSONstring.."}" return(JSONstring) end