Need help on serializing multiple objects for a datastore

  1. What do you want to achieve?
    A clothing shop and inventory with shirts, pants, accessories, character meshes, faces, etc bought with in-game currency.

  2. What is the issue?
    I am trying to make the purchased clothes save but since I cannot save instances in a datastore I am not sure how to serialize objects with different properties and load them into separate subfolders in their own category.

  3. What solutions have you tried so far?
    I have tried looking for solutions on the Developer Hub but so far haven’t found any

Everytime a player joins it creates a folder in ReplicatedStorage called “Player’s Clothes”. The clothes are seperated into different categories with subfolders and I am trying to find out how to save the purchased clothes inside the folders and load them into the folders when a player joins.
And since different objects have different properties I am not sure how to save them into one datastore

image

Thank you for reading.

Have you tried storing a table of the users clothing as their asset IDs? You can then just get that data and create the instances and set the asset ID. What properties exactly are different?

I am not sure how I would separate them into other categories.

For shirts the property is ShirtTemplate and for pants its PantsTemplate, it’s different for everything else like faces and character meshes and I am not sure how I would store them into one datastore and load them into different categories. I am not really good with datastores so I don’t understand how to do that.

I would store the ID and the ClassName (Shirt, Pants, etc) - you would still have to manually set the ID but there isn’t that many so it wouldn’t be a massive task.

I have gotten shirts to save but it wont save and load the pants because of different property names

-- Save
for i, obj in ipairs(clothes:GetDescendants()) do
	if obj:IsA("Shirt") then
		table.insert(data,{
			obj.ShirtTemplate,
			obj.Parent.Name,
			obj.Name
		})
	end
	if obj:IsA("Pants") then
		table.insert(data,{
			obj.PantsTemplate,
			obj.Parent.Name,
			obj.Name
		})
	end
end

-- Load
if success then
		local clothing = RS.OutfitEvents.PlayerClothes:WaitForChild(player.Name.."'s clothes")
		for i,cl in pairs(clothes:GetDescendants()) do
			if cl:IsA("Shirt") or cl:IsA("Pants") or cl:IsA("Decal") or cl:IsA("CharacterMesh") then
				cl:Destroy()
			end
		end
	for i, obj in ipairs(data) do
		local shirts = pcall(function() hasProperty(obj, "ShirtTemplate") end)
		if shirts then
			local s = Instance.new("Shirt")
			s.ShirtTemplate = obj[1]
			s.Parent = clothing:FindFirstChild(obj[2])
			s.Name = obj[3]
		end
		local pants = pcall(function() hasProperty(obj, "PantsTemplate") end)
		if pants then
			local s = Instance.new("Pants")
			s.PantsTemplate = obj[4]
		        s.Parent = clothing:FindFirstChild(obj[5])
			s.Name = obj[6]
		end
	end
else
	warn("Failed to load data: " .. tostring(err))
end