How do I get the players head accessories only?

So I have a script that takes the players user Id and gets all there accessories. Now the problem is that I only want there head accessories, so how do I just get them and not all of the other ones?

1 Like

Depending on how you retrieve the users accessories, if the accessory is an object there is a property called AccessoryType on the Accessory instance.

Here is a document page with the accessory type enumerations.
https://create.roblox.com/docs/reference/engine/enums/AccessoryType

In your script you could have a filter that filters specifically Hats, Hairs, and Face accessories only.
Iā€™m not sure on how your script retrieves the accessories so this could be a possible method.

(sorry to re-reply, i decided to attempt to code a working example)
here is a basic example of what you could do.

local players = game:GetService("Players")

local include = {
	Enum.AccessoryType.Face,
	Enum.AccessoryType.Hat,
	Enum.AccessoryType.Hair,
	Enum.AccessoryType.Eyebrow,
	Enum.AccessoryType.Eyelash
}

local function onJoin(player: Player)
	player.CharacterAdded:Wait()
	
	local function getOnlyHeadAccessories(outfit)
		local wearing = outfit:GetAccessories(true)
		local accessories = {}
		
		for index, struct in wearing do
			if struct.AccessoryType == nil then continue end
			if not table.find(include, struct.AccessoryType) then continue end
			
			table.insert(accessories, struct)
		end
		
		return accessories
	end
	
	local outfit = players:GetHumanoidDescriptionFromUserId(player.UserId)
	local list = getOnlyHeadAccessories(outfit)
	
	print(list)
end

players.PlayerAdded:Connect(onJoin)
for index, player in players:GetPlayers() do
	onJoin(player)
end
3 Likes

Ok this works! Now once I see if the accessory is a certain type of accessory how do I import it into roblox?

Thanks!

1 Like

You can use InsertService
https://create.roblox.com/docs/reference/engine/classes/InsertService

If you plan to use my script, you can change the code to something like this.

-- make sure you have this at the top
local insert = game:GetService("InsertService")

-- return to the print(list) line and replace it with something like this
for index, accessory in list do
	local content = insert:LoadAsset(accessory.AssetId)
	content.Parent = workspace

    -- insert service will return the asset which then you can
    -- do what ever you want with it
end
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.