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.
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
Viewports are a bit tricky. If there isn’t a native way within viewports to fix this then here are my suggestions:
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.
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.
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.
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.
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.
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