I made a save slot system in my game but I cannot figure out how to save the folder and its descendants to datastore2 when a player leaves.
I am trying to convert something like this to a table and then save it.
function data.save(player, abletosave)
local folder = player:WaitForChild("Save Slots")
if abletosave.Value == true then
local slots = {}
local SlotsStore = DataStore2("SaveSlots", player)
local id = 0
for i, slot in pairs(folder:GetChildren()) do
table.insert(slots, {slot.Name})
for _, Slot in pairs(slot:GetDescendants()) do
id += 1
if string.find(Slot.ClassName, "Value") and Slot.ClassName ~= "CFrameValue" then
table.insert(slots[i], {Slot.Name, Slot.Parent.Name, Slot.ClassName, {Slot.Value}, id})
elseif Slot.ClassName == "CFrameValue" then
table.insert(slots[i], {Slot.Name, Slot.Parent.Name, Slot.ClassName, {Slot.Value:GetComponents()}, id})
else
table.insert(slots[i], {Slot.Name, Slot.Parent.Name, Slot.ClassName, "no value", id})
end
end
end
SlotsStore:Set(slots)
end
end
This does convert it but it doesn’t sort it well. All the descendants in the save slot are in one table.
Load function:
function data.load(player, abletosave)
print("load")
local folder = player:WaitForChild("Save Slots")
local SlotsTable = {}
local SlotsStore = DataStore2("SaveSlots", player)
SlotsTable = SlotsStore:Get()
if SlotsTable then
for _, v in pairs(SlotsTable) do
local slotName = Instance.new("Folder", folder)
slotName.Name = v[1]
for _, b in pairs(v) do
if b[2] == slotName.Name then
local obj = Instance.new(b[3], slotName)
obj.Name = b[1]
for _, n in pairs(v) do
if n[5] == v[5] then
for _, m in pairs(n) do
local obj2 = Instance.new(m[3], obj)
obj2.Name = b[1]
end
end
end
end
end
end
end
abletosave.Value = true
end
If anyone can please help me with this since I have been spending almost a week trying to figure out this save slot system.