Saving players outfit to a datastore

The idea here is rather simple.

I have an avatar editor in my game, which allows for players to edit their avatar, however, the problem with this lies with the fact that I’d need a way to save players outfits.

I quickly found a rather easy way to save shirts and pants but when I got to accessories the problem became much greater, as the method I used to save shirts and pants causes major errors when trying to adjust it to accessories. It also simply isn’t elegant, the player can have a completely arbitrary amount of accessories and this method just doesn’t sit right with me.

You can see the method I had been using below

function SaveShirtAndPants(plr,outfitname)
	local char = plr.Character or plr.CharacterAdded:Wait()
	local urlpants = char:WaitForChild("Pants").PantsTemplate
	local urlshirt = char:WaitForChild("Shirt").ShirtTemplate
	-- print(urlshirt)
	-- print(urlpants)
	SaveOutfit(plr,urlshirt,urlpants,outfitname)
end

The problem also applies to animations and packages (and probably some other things like layered clothes)

I would normally use HumanoidDescription, then serialize HumanoidDescription to save the data, however HumanoidDescription does not actually reflect what the actual avatar in game looks like, it reflects the players avatar from the roblox website.

It is possible to append HumanoidDescription to reflect what the actual in game avatar looks like, then save the description like that and create a new instance for loading and use ApplyHumanoidDescription. This works very well for things like packages, animations, etc. But it’s not as simple for accessories, they’re separated into groups of accessories, i.e “Waist accessories” have a different value from “Hair accessories” and I would like to be able to avoid having to spam a lot of else if statements to get around this.

So at this point I’m somewhat stumped, I know that this is possible since I’ve seen other games do it, but for me at this point I think I need a little bit of help, if anyone has any better ideas than using HumanoidDescription please let me know! Or if anyone knows if I’m using HumanoidDescription wrong then that’d also be great.

Each accessory has an attachment which references the location of where it will be connected. For instance, a hair asset’s attachment under it will be called “HairAttachment” along with a weld that does the real magic. You can store the information about the accessories as an array. Unfortunately this does also mean that you will need to iterate through every object in the character, but this is the immediate solution that I can think of. Only certain data is relevant about the attachment, so it shouldnt be the worst experience to sort through it.
From the studio api:

1. -- Create the Accessory.
2. local clockworksShades = Instance.new("Accessory")
3. clockworksShades.Name = "ClockworksShades" --semi-relevant info
* local handle = Instance.new("Part")
4. handle.Name = "Handle"
5. handle.Size = Vector3.new(1,1.6,1) --relevant info
6. handle.Parent = clockworksShades
* local faceFrontAttachment = Instance.new("Attachment")
7. faceFrontAttachment.Name = "FaceFrontAttachment" --relevant info
8. faceFrontAttachment.Position = Vector3.new(0,-0.24,-0.45) --relevant info
9. faceFrontAttachment.Parent = handle
* local mesh = Instance.new("SpecialMesh")
10. mesh.Name = "Mesh"
11. mesh.Scale = Vector3.new(1,1.3,1) --relevant info
12. mesh.MeshId = "rbxassetid://1577360" --relevant info
13. mesh.TextureId = "rbxassetid://1577349" --relevant info
14. mesh.Parent = handle

From this there are 6-7 pieces of information you’ll store, and there are many ways to compress the data down if you need to.