Hello, I’m working on a game and at this moment in time, I’m struggling to create an efficient DataStore.
As shown in the image below - As players progress through the game, they will be able to capture creatures. Each time a new one is captured, a new Folder is added. Now I’m wondering how would I be able to DataStore all the new data recorded in ‘inCatalog’, ‘inParty’, and ‘inStorage’ as the Player Progresses?
Take a look at these function. getTableForm gets all the data from a folder and it’s descendants then puts it into a savable form. parseData takes that data and puts it back into its folder and value form. If you were to call getTableForm on EvolytData and saved the result you could then call parseData with EvolytData and the data again to generate the folders.
function parseData(parent, data)
if (data["obj"] == nil) then
return;
end
local contents = data["content"];
local object = Instance.new(data["obj"], parent);
object.Name = data["name"];
if (data["value"]) then
object.Value = data["value"];
end
if (contents ~= {}) then
--print(http:JSONEncode(contents));
for i,v in ipairs(contents) do
local newTable = {obj=v.obj,content=v["content"],name=v.name};
if (v.value) then
newTable["value"] = v.value;
end
parseData(object, newTable);
end
end
return object;
end
function getTableForm(parent)
local newData = {["obj"]=parent.ClassName,["name"]=parent.Name};
local newContent = {};
if (#parent:GetChildren() > 0) then
for i,v in ipairs(parent:GetChildren()) do
local tForm = getTableForm(v);
table.insert(newContent, tForm);
end
if (parent.ClassName ~= "Folder") then
newData["value"] = parent.Value;
end
newData["content"] = newContent;
return newData;
else
if (parent.ClassName ~= "Folder") then
newData["value"] = parent.Value;
end
newData["content"] = newContent;
return newData;
end
end
Here is my code that I use for transferring data between folders and tables:
-- Converting a folder to table form
function IterateThroughFolder(Folder)
local Table = {}
for i,v in pairs(Folder:GetChildren()) do
if #v:GetChildren() == 0 then
Table[v.Name] = v.Value
else
Table[v.Name] = IterateThroughFolder(v)
end
end
return Table
end
-- Converting a table to folder form
function TableToFolder(Table, Parent)
for i,v in pairs(Table) do
if typeof(v) ~= "table" then
local T = CapitalFirst(typeof(v))
if T == "BooleanValue" then
T = "BoolValue"
end
local Value = Instance.new(T)
Value.Name = i
Value.Parent = Parent
Value.Value = v
else
local F = Instance.new("Folder")
F.Parent = Parent
F.Name = i
TableToFolder(v, F)
end
end
return Parent
end