Attaching a hat to an NPC via catalog ID

I’m working on a NPC customization script that pulls from a table of catalog ID’s to randomly create the NPC’s appearance. So far I’ve been able to get the character’s clothing, face, and skin to work correctly. My last step is to add hats, specifically hair but I’m not able to find proper documentation on the dev website docs. I found the :AddAccessory function but I wasn’t sure on how to implant it.

The dev docs shows the developer manually inputting the position, size, and position. Is there a way to do this automatically with insert service or a type of function? Here is a link to the doc in question: Humanoid:AddAccessory

Is there something I’m missing with this or do I have to position each hat individually and clone it to the workspace?

You just have to load the asset using InsertService and use the function Humanoid:AddAccessory

local assetId = 7141674388
local NPC = workspace.Dummy
local InsertService = game:GetService("InsertService")
local model = InsertService:LoadAsset(assetId)
local accessory = model:GetChildren()[1]
NPC.Humanoid:AddAccessory(accessory:Clone())
2 Likes
local insert = game:GetService("InsertService")

local npc = workspace.Dummy
local humanoid = npc.Humanoid

local assetId = 0 --Change to accessory ID.

local success, result = pcall(function()
	return insert:LoadAsset(assetId)
end)

if success then
	if result then
		local accessory = result:FindFirstChildOfClass("Accessory")
		if accessory then
			humanoid:AddAccessory(accessory)
		end
	end
else
	warn(result)
end

Would be worth wrapping the LoadAsset() call in a pcall() as it can potentially error, if the issued API request times out/connection drops.