Insert Hat Accessory Onto Character

I am trying to load a character model into a ViewportFrame and I keep getting stuck at :AddAccessory() when loading in the character’s hats. It loads them into the game but it send them off into the oblivion, as you can see in the screenshot below.

image

How can I get the hats to go on the head like a normal character?
This is the current code I’m using, sorry about the messiness of it. preview is defined as a model of a character in the ViewportFrame. insert is InsertService.

local accessory = insert:LoadAsset(asset.id)
for i,v in pairs(accessory:GetChildren()) do
	if(v.ClassName == "Accessory")then
		preview.Humanoid:AddAccessory(v)
		break
	end
end
1 Like

Viewports are a bit tricky. If there isn’t a native way within viewports to fix this then here are my suggestions:

  1. Do it yourself. Yes I know we get lazy a lot but you could always CFrame it yourself. Try finding where the accessory would attach and put it right there.

  2. Move it to workspace, allow it to auto snap, then move it back. Workspace has this interesting property where physics and stuff just work. Usually when you equip an accessory to a Humanoid in workspace it will snap into place. So when you do that, move it back into the viewport.

So for this, you’re saying to use :AddAccessory() to a character in workspace, and bring it to the model in the ViewportFrame?

Working with character models on the client is very annoying, to say the least. If you’re using a local script (Which I assume you are) the client will not automatically create welds for accessory or anything added to the character model like it would on the server. You have to manually create welds and add them to the accessory, that connect the accessory and character model.

I’m using a server script that modifies the local GUI.

Have you ever considered turning the archivable property of the Character model on, cloning the players current character, turning it off and rendering them in an idle animation (or any animation)?

I do this, works a treat and it always works consistently.

Does this work for any character? I’ve never actually knew what the Archivable thing was for.

Well, providing the player is in the game yes.

Just be careful running the animation, make sure the clone has its Primary Part CFrame set to something out the way, CFrame.new() for example - run the animation whilst its in the workspace ( somake the clone a descendant of the workspace so it uses the workspace properties) and then run the animation, then put it into your viewport model and bam you can set the Camera centered on this.

Archivable is basically just what stops replication, by turning it off we can yoink the character.

When I try this I get Attempt to index nil with 'Archivable' or something of the sort.
char is the player’s character.

char.Archivable = false
local prev = char:Clone()
char.Archivable = true
prev.Parent = workspace

You shouldn’t call it “char” :grimacing:

The character also may not exist yet, so you’d be advised to wait for it or search for it through workspace.

There’s probably a much better way to do this but it’s what I first thought of at the time while also doing other things, but it just hangs there or errors when I tried anything.
This is what I mainly tried.

character.Archivable = false
local newcharacter = character:Clone(workspace)
while newcharacter == nil do wait(.1) end
character.Archivable = true

I’ll upload a place file of the system I use, rarely do this but eh. :slightly_smiling_face:

Here:
viewport_new.png.rbxl (32.4 KB)

You’ll find the animation for it inside as well.

1 Like

Sorry for the really late response, but your code works really well. I won’t use it piece by piece but it’s a good place for starting.

1 Like