I can't figure out how I can save this to datastore2

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.
img-2024-06-27-18-42-49

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.
img-2024-06-27-18-51-50

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.

can you show an entire index’s heirarchy in output. to me it looks like you’re handling it properly so i do not understand the issue

Sorry if this is a little hard to understand, but I will try to explain it the best I can.

So inside the “Save Slot 1,” there are the “foil” blocks. Inside the foil block tables, there is supposed to be another table, which is where the settings folder should go and would contain the Transparency, CanCollide, and Anchored values.


When it loads, I have trouble getting the objects to load into the correct folders because I can’t simply check if the blocks have the right name. There are multiple blocks with the same name, and there’s a chance that the values could load into the wrong folder.

perhaps recode it so that you check each folder and recurse

local slots = {}

local function recursefunction(parentTable, outputTable, counter)
	--index 0 is just to initialize the first block i.e. "light"
	local StartingTable
	if counter == 0 then
		local targindex = #slots+1
		
		slots[targindex] = {}
		--light
		slots[targindex][parentTable.Name] = {}
		StartingTable = slots[targindex][parentTable.Name]
	end
	
	for i, v in pairs(parentTable:GetChildren()) do
		if #v:GetChildren() > 0 then
			local newTable = {}
			if counter == 0 then
				StartingTable[v.Name] = newTable
			else
				outputTable[v.Name] = newTable
			end
			
			recursefunction(v, newTable)
		else
			if counter == 0 then
				table.insert(StartingTable, v.Name)
			else
				table.insert(outputTable, v.Name)
			end
			
		end
	end
end

workspace:WaitForChild("Light")

recursefunction(workspace.Light, slots, 0)

print(slots)

output

▼  {
                    [1] =  ▼  {
                       ["Light"] =  ▼  {
                          [1] = "cfval",
                          [2] = "id",
                          ["Settings"] =  ▼  {
                             [1] = "boolval"
                          }
                       }
                    }
                 }  -  Server - Script:39

though the code is ugly i think you get the general jist that you need to traverse over the table in a sequential order and apply indexes properly

seems like your issue is more related to how you’re structuring your data, i think this is not complicated if properly using dictionaries.

should also learn about serialization:

I still don’t understand how I can get it to work with folders, intvalues, bool values, and cframe values though.