How to apply hat accessories from HumanoidDescription to a Humanoid

Hello, Mad here. I am currently trying to apply hats from a Player’s Avatar to my own StarterCharacter.
The problem is that I am not sure how to proceed.

I have tried using GetHumanoidDescriptionFromUserId to get a list of accessories, but when I run the script, I get the error, “Unable to cast value to object”.

The following script below is placed in StarterCharacterScripts.

local Character = script.Parent:WaitForChild("Humanoid").Parent
local Player = game.Players:GetPlayerFromCharacter(Character)
local UserId = Player.UserId

local HumanoidDesc = game.Players:GetHumanoidDescriptionFromUserId(UserId)
local Hats = HumanoidDesc.HatAccessory

Character.Humanoid:AddAccessory(Hats)

Any solution for what I’m trying to do?
Thanks

2 Likes

Solved it myself by just applying the whole description, then deleting everything I don’t need except for accessories and also changing the skin colour as well.

2 Likes

Sorry to bump, but how would you remove everything you don’t need?

(Does anyone else know)

I opened my debugger to learn more:

image

This explains the OP’s:

Unable to cast value to object

You are just 2 layers of indirection from fixing this:

    local HumanoidDesc = Players:GetHumanoidDescriptionFromUserId(player.UserId)
    local accessorySrc = game.Players:CreateHumanoidModelFromDescription(HumanoidDesc, Enum.HumanoidRigType.R15)
    local InsertService = game:GetService("InsertService")
    local hatAcc = InsertService:LoadAsset(HumanoidDesc.HatAccessory)
    -- Still not the real hat accessory. We need:
    local validHatAcc = hatAcc:FindFirstChildWhichIsA("Accessory")
    Character.Humanoid:AddAccessory(validHatAcc)

Without hatAcc:FindFirstChildWhichIsA("Accessory") we get:

AddAccessory should be passed a valid Accessory object.

because we have passed a model with the following descendents

{
                    [1] = BoySpaceHair,
                    [2] = Handle,
                    [3] = Mesh,
                    [4] = HatAttachment,
                    [5] = ThumbnailCamera
}

We want BoySpaceHair, in this case.

I’ve tested this. I wanted to add all accessories, but this is a start.

3 Likes