字典求助/DataStore

问题是我有一个包含所有数据的字典,它应该能够将其转换为在复制存储中的目录,所有值都是字符串,然后当玩家离开时将所有键转换回字典。但是,我无法想出如何将它转换为带有键的字典。

我已经测试了几个小时,但在第一层值之后,我无法想出一种将更深层次的值和键转入表格的方法。

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 函数,并将复制存储中的所有实例转换回带有键的字典。

点赞
用户12328451
用户12328451

如果其他人有这个问题,我用的解决方案是把实例直接转换成 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
2019-11-07 18:52:55