Welding accessories to a character

Hello dev forum members,

So, I have spent now the last 2 hours doing what I literally thought was supposed to be the easier part of making a character creator, which was attaching an accessory to a player.

Turns out, :AddAccessory() only works on the server side, does anyone have an alternative I can use to manually weld it to the player?

2 Likes

Just to clarify, is the accessory itself created by the server and you’re trying to weld it client-side?

No, its stored in a folder on the client side, and I clone it and try to attach it.

1 Like

So any ideas? I keep trying different things and its not working out. Can I use a weld, and if so, how?

Here’s a post from 2017 describing the same issue, with a solution written by one of the staff. It welds the accessory to the character.

Just fyi I just searched “AddAccessory local” using the forum’s search function. Search the forums before posting questions! It would be helpful if you told us what you looked up, tried, etc. in the OP in the future.

There’s also no point in that second reply apart from bumping the thread.

My solution, which I wrote before I saw the staff's solution... Oof.
local function AddAccessoryLocal(humanoid, accessory)
	local accessoryAttachment = accessory:FindFirstChildWhichIsA("Attachment", true)
	if not accessoryAttachment then
		warn("No attachments found in accessory. Accessory was not attached.")
		return
	end
	
	local attachmentName = accessoryAttachment.Name
	local character = humanoid.Parent
	local attachTo = character:FindFirstChild(attachmentName, true)
	if not attachTo or not attachTo:IsA("Attachment") then
		warn(string.format("No attachment named %s found in character. Accessory was not attached.", attachmentName))
		return
	end
	
	local disp = attachTo.WorldCFrame:toObjectSpace(accessoryAttachment.WorldCFrame)
	local Handle = accessory:FindFirstChild("Handle")
	if not Handle then
		warn("Attachment has no handle. Accessory was not attached.")
		return
	end
	
	accessory.Parent = character
	
	local weld = Instance.new("Weld")
	weld.Name = "AccessoryWeld"
	weld.Part0 = Handle
	weld.Part1 = attachTo.Parent
	weld.C0 = accessoryAttachment.CFrame
	weld.C1 = attachTo.CFrame
	weld.Parent = weld.Part0
end
2 Likes

@XAXA Does your version also work? Cause I did see that other thread you posted, and I couldn’t figure it out.

The function you want to call is function addAccoutrement(character, accoutrement)

character would obviously be the character (not the Humanoid, this would be a model!) you want to parent the accessory into and accoutrement is the accessory you want to make the character wear.

(and again, you should have told me that you tried that in the OP)

Tried it, it did not work for me.

I tried it and it works.

Show us your code. Tell us any errors.

You tried it on client side with the char locally? If so may I see your code and compare it to mine.

I literally just called the function on the client with my character and then an accessory on workspace (which was created by the client).

Please don’t make this any harder than it needs to be. Just show us your code.

1 Like

Alright I’ll try it and see if it works again.

local hair = game.Players.LocalPlayer.PlayerGui.CharacterItems.Hair:FindFirstChild(item.ItemName.Text)
local hairClone = hair:Clone()
--hairClone.Parent = gui.CharacterCustomize.CharacterFrame.BlankCharacter
addAccoutrement(gui.CharacterCustomize.CharacterFrame.BlankCharacter,hairClone)

@XAXA Yeah it doesn’t weld it at all, the part0 and part1 are blank.

UPDATE: You can use a WorldModel for this instead of positioning the accessory on the character manually. The solution in this post was written before WorldModels were added to the engine.


FINALLY. The issue here is that BlankCharacter is in a ViewportFrame and physics isn’t simulated in viewportFrames, therefore welds won’t work.

Instead of using a weld, you can try using CFrame to manually position the hat inside the model itself.

local function CFrameAccessoryToCharacter(characterModel, accessory)
	local accessoryAttachment = accessory:FindFirstChildWhichIsA("Attachment", true)
	if not accessoryAttachment then
		warn("No attachments found in accessory. Accessory was not attached.")
		return
	end
	
	local attachmentName = accessoryAttachment.Name
	local attachTo = characterModel:FindFirstChild(attachmentName, true)
	if not attachTo or not attachTo:IsA("Attachment") then
		warn(string.format("No attachment named %s found in character. Accessory was not attached.", attachmentName))
		return
	end
	
	local Handle = accessory:FindFirstChild("Handle")
	if not Handle then
		warn("Attachment has no handle. Accessory was not attached.")
		return
	end
	
	accessory.Parent = characterModel
	
	Handle.CFrame = attachTo.WorldCFrame * accessoryAttachment.CFrame:Inverse()
end

CFrameAccessoryToCharacter(workspace.XAXA, workspace.BeatUpTVCock)
18 Likes

Could I also take the character out of the view port frame temporarily, use the code from Gamer101 and then put it back in?

If you wait for a frame before putting it back in, sure.

EDIT: Oh, and parent it to workspace. Not just anywhere. Physics is only simulated in workspace.

1 Like

@XAXA Does physics run anywhere else? Cause parenting it to workspace puts it in a random place and I don’t want players to be able to see it.

No. Move the character somewhere hidden if you have to. If you don’t want to do that, then you have to go with the other solution (CFraming the accessory manually inside the ViewportFrame).

1 Like