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.
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.
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.
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
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.
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
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
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
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)