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)