I’m looking for a way to DataStore all contents within the ‘PlayerData’ folder -
Notably new Folders will be added / removed from the Folder during Gameplay and these will also need to Save - I’ve had a method in the past of doing this:
I’m trying to achieve this method again - However I’m having no luck in getting the code to work?
Thank you. ^^
When I datastore a lot of things, i use tables. You can also save tables inside of tables.
For example, create a table for the Player Data and have other tables for the the other data and save that inside of the Player Data table. Let me show you an example:
(I am gonna show you how to save the _Misc data, but the other data such as Inventory, Storage, Party, etc…would be the same. Remember that you can save multiple tables inside of a table
Players.PlayerRemoving:Connect(function(plr)
local PlrData = {}
local MiscData = {}
table.insert(MiscData, 1, Chunk.Value) -- Inserts the Chunk value into the table and 1 is the position in the table
table.insert(MiscData, 2, Storyline.Value)
table.insert(PlrData , MiscData) --Inserts the MiscData table into the PlrData table. As I said, you can save tables inside of tables. So follow this process with your other data and just make more tables
local success, fail = pcall(function()
DataStore:SetAsync(plr.UserId.."-data", PlrData ) --Saves the PlrData table
end)
if success then
print("Save success!") -- Data is saved
else
warn("Error saving for "..plr.Name..". "..fail)
end
end)
Checking the data when the player joins is pretty simple:
Players.PlayerAdded:Connect(function(plr) --Runs when a player joins
local PlrData = {} --Makes a new table
local success, fail = pcall(function()
PlrData = DataStore:GetAsync(plr.UserId.."-data") --Gets the player data if it exists and puts it into the table we just made
end)
if success then --if the player data exists
Chunk.Value = PlrData [1] --Makes the Chunk value the same as it was before the player left
Storyline.Value = PlrData[2]
end
end)
I wrote this stuff pretty quickly so I can imagine there might be some typos and I might’ve got some stuff wrong so let me know if you need help with anything and tell me if you need me to explain anything
Don’t represent data using instances like that. Make a proper data module that saves each player’s data in a table using a module like ProfileService and then use something like ReplicaService for state changes.
The way I would approach this is by making the data into a tree within a table.
You could do something like this:
-- ModuleScript
local function appendToTree(tree, child)
if child:IsA("Folder") then
tree[child.Name] = {}
else
-- Account for other classes
tree[child.Name] = child.Value -- Blah, blah
end
end
return function(DataFolder: Folder)
local tree = {}
-- You only need 4 nested loops because your hierarchical depth is 4.
for _, child in DataFolder:GetChildren() do
appendToTree(tree, child)
-- Continue if the child doesn't have any children (a.k.a is not a folder)
if not child:IsA("Folder") then continue end
for _, _child in child:GetChildren() do
appendToTree(tree[child.Name], _child)
if not _child:IsA("Folder") then continue end
for _, __child in _child:GetChildren() do
appendToTree(tree[child.Name][_child.Name], __child)
if not __child:IsA("Folder") then continue end
for _, ___child in __child:GetChildren() do
appendToTree(tree[child.Name][_child.Name][__child.Name], ___child)
end
end
end
end
return tree
end
Of course, this is a very hacky and unreliable algorithm. To make this more dynamic, you will need a way to figure out the hierarchical depth of an instance (which means the amount of descendants to the farthest descendant), which I do not have time to come up with. Hope this helps!