How to attach an Accessory?

How do I get this accessory
image

To attached properly to this Part
image

I know I use a WeldConstraint to weld it. The question is how do I get the Accessory in the right relative offset position to weld. The attachments btw are pulled straight out of the default character in studio.

I tried doing

Accessory.Handle.CFrame = Head.CFrame * Head.HairAttachment.CFrame

and

Accessory.Handle.CFrame = Head.CFrame * Accessory.Handle.HairAttachment.CFrame

Neither of those put it in the same spot as Humanoid:AddAccessory.

So the question is how does Humanoid:AddAccessory calculate the position?

Also why are there no WeldConstraints in the default player character? The parts magically stick to each other?

Sorry if the answer is obvious I don’t really know how Attachments and Constraints and those things work

thanks.

4 Likes

Don’t you just use the Attachment? That should work…

Thankfully Roblox now has a method for humanoid’s to do precisely this.
Humanoid:AddAccessory(Your_Accessory)

https://developer.roblox.com/en-us/api-reference/function/Humanoid/AddAccessory

It places it based on where the Attachment is.

Code from the devhub:

local faceFrontAttachment = Instance.new("Attachment")
faceFrontAttachment.Name = "FaceFrontAttachment"
faceFrontAttachment.Position = Vector3.new(0,-0.24,-0.45)
faceFrontAttachment.Parent = handle

The accessory’s will be placed such that the frontFaceAttachment meets the one inside the player’s head.

I don’t have a Humanoid, that’s why I am asking.

Which attachment? look at the code I tried, it did not work.

Please elaborate

Make an attachment inside your handle. Give the attachment a name. This name should match an attachment inside the player’s character of which you wish to align with your attachment.

image

You mean exactly like this? It’s already like that.

Again sorry, I still don’t know how attachments work.

Yes like that. Here is an example of a ugc hair which does it the same way:
image

Okay now how do I get it to be in the right CFrame?

All characters have a humanoid which is why you state this in a LocalScript in StarterCharacterScripts:

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character.Humanoid

Humanoid:AddAccessory(game.Workspace.AccessoryName_)

Adjust the position of your HairAttachment

LOL just figured it out!

Nevermind I had a Humanoid in the part so it auto positioned it rip

oh yeah I didn’t notice that in your original code. my bad

@DrKittyWaffles What do you mean adjust it? it’s already set to the right offset because I took it directly from studio.

What’s the code that Humanoid:AddAccessory uses to offset the Accesory handle? Because it’s not the code that I tried.

I might just add a Humanoid just so I can use Humanoid:AddAccessory() if I can’t figure it out

Accessories snap in place by their attachments’ CFrames.

local accessoryWeld = Instance.new("Weld")
accessoryWeld.Name = "AccessoryWeld"
accessoryWeld.Part0 = YOUR_ACCESSORY_HANDLE
accessoryWeld.Part1 = LIMB_TO_ATTACH_TO
accessoryWeld.C0 = accessoryWeld.Part0.ATTACHMENT_NAME.CFrame
accessoryWeld.C1 = accessoryWeld.Part1.ATTACHMENT_NAME.CFrame
accessoryWeld.Parent = accessoryWeld.Part0

Don’t use a WeldConstraint since you can’t change the offset.

EDIT 12/03/2021: Use RigidConstraints to snap accessories together by their attachments instead.

10 Likes

Thank you! This is what I am looking for!

I have another question that came up. The accessory Handle is making a TouchTransmitter anytime I parent it to something in workspace, so if my character Touches a Humanoid, then the accessory gets yoinked from my character and gets placed on the Humanoids character.

Nevermind I figured it out. Anytime there is a BasePart named “Handle” in an accessory it will create the TouchTransmitter. All I have to do is rename “Handle” to “handle”.

You could also just turn off CanTouch on the part. TouchTransmitter facilitates Touched events and it’s illegal to add one to a BasePart that has CanTouch off (a warning will be sent to the console). Depending on your circumstances you could also destroy it on the same frame its created by checking for a new instance of the TouchTransmitter class but that’s less elegant than just turning off CanTouch.

1 Like

Okay thanks. I tried deleting the TouchTransmitter when it gets created using a ChildAdded event but I got a warning that says “Something unexpectedly tried to set the parent of TouchInterest to NULL while trying to set the parent of TouchInterest. Current parent is Handle.”

Setting CanTouch works good and is less hacky than renaming the Handle, I’ll use that!

You would need to defer the destruction not to get that error.

ChildAdded:Connect(function (child)
    if child:IsA("TouchTransmitter") then
        task.defer(child.Destroy, child)
    end
end)

This does mean more connections though and that’s not good for memory management especially if you don’t predictably know how many accessories may end up needing that deletion. CanTouch false is definitely the better route here.

1 Like