Saving folder and contents to datastore, help needed

Hello, I am currently trying to work out how I would be able to save a folder and its contents to a datastore.

What I am trying to achieve is that a player can have their own sort of “documents” folder in the game, but I have no idea how to save this to a data store, which is required so they don’t lose their data. Also, I cannot just store them all one by one, as I do not know the names of the player’s folders, etc.

If anyone could help me with a method to save a whole folder and its contents (checking if the children of the folder have children, etc) and to a datastore then your help would be highly appreciated.

2 Likes

Where is the “documents” folder going to be located? In the player? Also what exactly is the folder going to be holding? Ill try my best to help just need the answer to those two and maybe other people need answers too.

2 Likes

The folder will be located in server storage, in a parent folder and under than parent folder will be a players user ids who are currently in the server.

The folders will be containing mostly other folders and string IDs, with a couple of exemptions for other instances.

1 Like

if that’s all that’s in the folders, you can loop through recursively using the .Name property to get the folders name, and the .Value property to get the contents of the StringValue

How would I keep looping through the folders until there is no children?

1 Like

You just do

for i,v in pairs(folder:GetChildren()) do

to loop through a folder, then get the values inside the folder and set to your data.

I guess you can save these objects, by storing their properties and informations about them in a table, organize it the way you should, and save that table.
So to get the picture

local folder = {}

for i, v in pairs (ServerStorage.Folder:GetChildren()) do
    folder[v.Name] = {v.Transparency, v.BrickColor, v.Reflectance}
end

I know how to loop through a folder, but i’m pretty sure that will just loop once through that folders children and not the child of the folders children and keep on going until there are no children found.

1 Like

Than you can use :GetDescendants() instead, which will loop through all of them, its children, and the children of its children, and so on.

for i, v in pairs (ServerStorage.Folder:GetDescendants()) do
  
end

A descendant is pretty much the child of the child (and so on) of something

I store all of my data within a table, but if you insist on using folders this might work.
If you have folders inside of folders, you can use :GetDescendants() do get everything inside the folder.

for i, instance in pairs(folder:GetDescendants()) do
    if not instance:IsA("Folder") then
        -- will print every single value in the main folder.
    end
end
1 Like

How would I then put them back all in order, when I load the data from the folder?

1 Like

Well it should be the same order, since whatever is getting looped first is the first object, than when you loop again to get the stuff back, the first thing will be first again I assume

And you can’t save objects using datastore, you should follow the table method

I still don’t get how I would piece it all back together.

1 Like

Yes, I will be following the table method.

1 Like

You can have a folder preset, so you just clone it and parent it.

local PlayerData = {} -- or whatever you get data from (getasync, etc)
local folder = DataFolder:Clone() -- preset datafolder, just an example
for data, value in pairs(PlayerData) do
    for i, instance in pairs(folder:GetDescendants()) do
        if instance.Name == data then
            instance.Value = value
        end
    end
end
folder.Parent = player -- parent the main folder
2 Likes

Ah, thank you for your reply to my topic.
Will mark as solution if it works.

on mobile but something along the lines of this for recursively getting children, presuming you want to keep subfolders

local search
search = function(inst)
    for a,b in pairs(inst:GetChildren()) do
        if (b:IsA 'Folder') then
            -- could do stuff here
            search(b)
        else
            -- do stuff
        end
    end
end

search(game.ServerStorage.PlayerFolder)