View port Frame Accessories

Is there a way to add Accessories to a character inside a view port frame? so far nothing I’ve tried works when I use humanoid:AddAccessory() is just spawns behind the character for some reason
(This is for a character customization)

1 Like

You can implement your own AddAccessory function, like here:

-- void addAccessory(Model character, Accessory accessory)
-- Adds an accessory to the character model.
local function addAccessory(character, accessory)
	local attachment = accessory.Handle:FindFirstChildOfClass("Attachment") -- Not all Accessories are guaranteed to have an Attachment - if not, we'll use default hat placement.
	local weld = Instance.new("Weld")
	weld.Name = "AccessoryWeld"
	weld.Part0 = accessory.Handle
	if attachment then
		-- The found attachment name in the accessory matches an existing attachment in the character rig, which we'll make the weld connect to.
		local other = character:FindFirstChild(tostring(attachment), true)
		weld.C0 = attachment.CFrame
		weld.C1 = other.CFrame
		weld.Part1 = other.Parent
	else
		-- No attachment found. The placement is defined using the legacy hat placement.
		weld.C1 = CFrame.new(0, character.Head.Size.Y / 2, 0) * accessory.AttachmentPoint:inverse()
		weld.Part1 = character.Head
	end
	-- Updates the accessory to be positioned accordingly to the weld we just created.
	accessory.Handle.CFrame = weld.Part1.CFrame * weld.C1 * weld.C0:inverse()
	accessory.Parent = character
	weld.Parent = accessory.Handle
end

So in case you’re trying to use Humanoid:AddAccessory() from a LocalScript or the like, this implementation should work, while the Humanoid:AddAccessory() method will only work server-side IIRC. :slightly_smiling_face:

7 Likes
2 Likes

@TheGamer101 Do we have any updates on AddAccessory() working properly with a LocalScript?

I’ll file a ticket to look into this. I tried to make this work correctly before but it wasn’t easy to get working properly without causing a possible desync in how the client/server sees the Accessory. Developer code knows whether the accessory is visible to the server or not so it can avoid creating duplicate welds on the client but it’s difficult to make Accessories handle both cases correctly by default.

2 Likes

Alright, glad to hear that there’s progress on it. :+1:


For now we’ll use the code you provided.

The way I got it to work was with a server event that would put the model in the workspace then add the accessory and then put it back in the view port. this looked bad since it’d flicker so I just made the customization stay in the game for now