What is the best way to get a players accessories in game? This include hats, layered clothing, classic clothing, and anything else accessory related. I would like to get the IDs of all of these things that a player might be wearing in game. What is the best way to do so?
I started an attempt below but it does not get everything. It is missing some of my accessories like my layered clothing and shirts
local Char = Plr.Character;
local humanoid = Char.Humanoid;
local humanoidDescription = humanoid:GetAppliedDescription()
local accessoryProps = {"HatAccessory","HairAccessory","FaceAccessory","NeckAccessory","FrontAccessory","BackAccessory","WaistAccessory","ShouldersAccessory"}
for _, prop in ipairs(accessoryProps) do
if humanoidDescription[prop] ~= nil then
local idsString = humanoidDescription[prop]
if idsString and idsString ~= "" then
for _, id in ipairs(string.split(idsString, ",")) do
print(Plr.Name .. " has accessory ID:", id, "(type:", prop .. ")", pt)
local newTemplate = Templates["Item"]:Clone()
newTemplate.Name = id
newTemplate.Image = ("rbxthumb://type=Asset&id=" .. tostring(id) .. "&w=420&h=420") --Put image of id
newTemplate.Parent = scrollingFrame
end
end
end
end
for _, asset in pairs(Char:GetChildren()) do
if asset.ClassName == "Pants" then
local newTemplate = Templates["Item"]:Clone()
newTemplate.Name = asset.Name
newTemplate.Image = asset.PantsTemplate
newTemplate.Parent = scrollingFrame
end
end
You can use HumanoidDescription:GetAccessories(true) to get an array of all the player’s worn accessories, including layered clothing. The asset ids of their classic clothing can also be found on the description, but this is the asset id, not the template id. The simplest way to get the template id is to use InsertService:LoadAsset(clothingAssetId), which will create a model containing a shirt/pants/tshirt instance, whose template id you can read.
--[[ Variables ]]--
-- Services --
local Players = game:GetService("Players")
-- Types --
type AccessoryData = {
AccessoryType : Enum.AccessoryType,
AssetId : number,
IsLayered : boolean,
Position : Vector3,
Rotation : Vector3,
Scale : Vector3
}
--[[ Functions ]]--
local function getClassiClothingIdsFromDescription(description : HumanoidDescription) : {}
return {
Shirt = description.Shirt,
GraphicTShirt = description.GraphicTShirt,
Pants = description.Pants,
}
end
local function getAccessoryDataFromDescription(description : HumanoidDescription) : ({AccessoryData}, {AccessoryData})
local accessories : {AccessoryData} = description:GetAccessories(true)
local rigidAccessories = {}
local layeredClothing = {}
for _, v in accessories do
if v.IsLayered then
table.insert(layeredClothing, v)
else
table.insert(rigidAccessories, v)
end
end
return rigidAccessories, layeredClothing
end
local function getClothingTemplateId(assetId : number) : number
local insert = InsertService:LoadAsset(assetId)
local clothing = insert:FindFirstChildWhichIsA("CharacterAppearance")
local url : string
if clothing:IsA("Shirt") then
url = clothing.ShirtTemplate
elseif clothing:IsA("Pants") then
url = clothing.PantsTemplate
elseif clothing:IsA("ShirtGraphic") then
url = clothing.Graphic
end
local id = string.match(url, "%d+")
return tonumber(id)
end
local function getPlayerAppearanceData(userId : number) : ()
local description = Players:GetHumanoidDescriptionFromUserId(userId)
local classicClothing = getClassiClothingIdsFromDescription(description)
local accessories, layeredClothing = getAccessoryDataFromDescription(description)
print(classicClothing)
print(layeredClothing)
print(accessories)
end
The getClothingTemplateId() function uses the InsertService method I proposed above to get the template id of a given classic clothing asset.
Technically, you don’t “wear” bundles, you wear the items contained in the bundle, and as far as I know, there isn’t a way to get a bundle from an asset id, but if you’re asking how to get the asset id of body parts, they’re also accessible from the humanoid description directly.