#How can I make my DataStore save the hierarchy of the objects?

1 messages · Page 1 of 1 (latest)

fading pulsar
#

So I have a DataStore right here, (which i hope that it works) and since I'm making a file system, i need the hierarchy of the objects. How can I do that?

local DSS = game:GetService("DataStoreService")
local dataStore = DSS:GetDataStore("FileSave")

local keyPrefix = "Player: "
local fileSystem = game.ReplicatedStorage.files

-- Functions
local function save(player:Player)
    local key = keyPrefix .. player.UserId
    local data = {}

    for _, object in ipairs(fileSystem:GetDescendants()) do
        if object:IsA("StringValue") then
            table.insert(data, {
                Name = object.Name,
                Class = object.ClassName,
                Value = object.Value,
                IsEditable = object:GetAttribute("IsEditable"),
                IsSecret = object:GetAttribute("IsSecret"),
                ParentPath = object.Parent:GetFullName()
            })

        elseif object:IsA("Folder") then
            table.insert(data, {
                Name = object.Name,
                Class = object.ClassName,
                IsVisible = object:GetAttribute("IsVisible"),
                ParentPath = object.Parent:GetFullName()
            })
        end
    end

    local success, err = pcall(function()
        dataStore:SetAsync(key, data)
    end)
end

local function load(player:Player)
    local key = keyPrefix .. player.UserId
    
    local success, err
    local data
    repeat
        success, err = pcall(function()
            data = dataStore:GetAsync(key)
        end)
    until success or not game.Players:FindFirstChild(player.Name)
    
    if not data then return end
    
    if not success then
        warn("Failed to load data, got error " .. tostring(err))
    end
end

-- Firing

game.Players.PlayerAdded:Connect(load)
game.Players.PlayerRemoving:Connect(save)

game:BindToClose(function()
    for _,player in game.Players:GetPlayers() do
        save(player)
    end
end)
grizzled fossil
#

basicly you need to make a magic table to get all the datas of your tree and turning it to a flat instance list (or one in the other)

grizzled fossil
#

its like just a table with the list of property you want to save then you iterate in it get everyproperty and save it

grizzled fossil
fading pulsar
#

Ok