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)
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.
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