How would I add a hat to an NPC and get it into position?

Right so basically I have this,

local hats = 0
for i,v in pairs(HatsFold:GetChildren()) do
	hats = hats + 1
	
	local AssetId = v.ID.Value
	
	local ClonedButton = script.Parent.Faces.Template:Clone()
	ClonedButton.Visible = true
	ClonedButton.Name = "Face"..hats
	ClonedButton.Image = "https://www.roblox.com/asset-thumbnail/image?assetId="..AssetId.."&width=420&height=420&format=png"
	ClonedButton.Parent = script.Parent.Hats
	
	ClonedButton.MouseButton1Click:Connect(function()
		local Hat = v:Clone()
		Hat.Parent = game.Workspace.JoinDummy
	end)
end

When ClonedButton is clicked I want it to put the hat (v in our case) onto the NPC. Luckily it clones the hat into the character, but it’s not in position.


The grey part is a camera part.
The grey dude is the NPC, and on the left is the 2 hats I’m using for tests.

Thanks for any help!

Are you using parts for your hats or the accessory class?

I’m using accessories. chars

Consider using the function Humanoid:AddAccessory()

That does not work locally. Everything that is being done in my script is local.

you are going to need to weld the hats to the NPC the method i would use to do this is to get each of your hats and Weld a “weld part” to them.

so get a dummy and a part.
image

name the part “WeldPart” and the dummy “WeldPartDummy” type this into Command Bar:
game.Workspace.WeldPart.CFrame = game.Workspace.WeldPartDummy.Head.CFrame
this will position the part perfectly in the head
image

the Put you Hat Above the Head
image

Now weld the “WeldPart” to the hat and group them together
when the “ClonedButton” is clicked position the weld part to the head of the NPC and weld the WeldPart to the Head of the NPC this will atoumatically put the hat in the right position and keep it attached. if you have any questions or didnt understand anything feel free to ask.

4 Likes