Attempting to create a save/load player avatar script

Hello, im currently stuck on how I am able to save a list of accessorys and clothing the player has on there avatar and im also currently stuck on how im gonna be able to load it onto a player character.

local DataStoreService = game:GetService("DataStoreService")
local charDataStore = DataStoreService:GetDataStore("Character")

script.Parent.SaveEvent.OnServerEvent:Connect(function(plr)
	local plrID = 'Player_'..plr.UserId
	
	local hats
	local shirt
	local pants
	local face
	
	local hatstable = {}
	local faceid = 0
	
	for _,Hat in pairs(plr.Character:GetChildren()) do
		table.insert(hatstable,Hat.Name)
	end
	
	for _,face2 in pairs(plr.Character:GetChildren()) do
		if face2:IsA("Decal") then
			face2.Texture = faceid
		end
	end
	
	local info = {
		hatstable;
		face = faceid
	}
	
	local success, errormessage = pcall(function()
		charDataStore:SetAsync(plrID,info)
	end)
	
	if success then
		print('saved')
	else
		warn(errormessage)
	end
	
	script.Parent.LoadEvent.OnServerEvent:Connect(function(plr)
		plr.CharacterAdded:Connect(function()
			
		end)
	end)
end)

This should work now

local DataStoreService = game:GetService("DataStoreService")
local charDataStore = DataStoreService:GetDataStore("Character")

script.Parent.SaveEvent.OnServerEvent:Connect(function(plr)
    local plrID = 'Player_' .. plr.UserId

    local hats = {}
    local faceIds = {}

    for _, Hat in pairs(plr.Character:GetChildren()) do
        if Hat:IsA("Accessory") then
            table.insert(hats, Hat.Name)
        elseif Hat:IsA("Decal") then
            table.insert(faceIds, Hat.Texture)
        end
    end

    local info = {
        hats = hats,
        faceIds = faceIds
    }

    local success, errormessage = pcall(function()
        charDataStore:SetAsync(plrID, info)
    end)

    if success then
        print('Saved')
    else
        warn(errormessage)
    end
end)

script.Parent.LoadEvent.OnServerEvent:Connect(function(plr)
    local plrID = 'Player_' .. plr.UserId

    local success, data = pcall(function()
        return charDataStore:GetAsync(plrID)
    end)

    if success and data then
        local hats = data.hats
        local faceIds = data.faceIds

        -- Apply hats and faceIds to the player's character
        -- Example code:
        local character = plr.Character
        for _, hatName in ipairs(hats) do
            -- Insert code to add hats to the character
        end
        for _, faceId in ipairs(faceIds) do
            -- Insert code to apply faceIds to the character
        end

        print('Loaded')
    else
        warn('Data not found')
    end
end)

Ok, ill add the additional code and then ill tell if something went wrong.

I did a litttle debugging and so,


The lines of codes commented are some i’ve tried but i’ve never found a way. Are these simply the names of these hats? or is there more.
I also printed the classname and found out that it was nil.

Maybe its because

hats.Parent = plr.Character
--hats is the table you should instead try:
hatName.Parent = plr.Character

image