CFrame welding, trying to attach an object to the players leg

When I rotate my character in any direction, it’ll weld incorrectly. To be specific, it’ll weld on the back of the leg because the script doesn’t know how to factor in offsets or whatever CFrame magic will make this problem go away.

return function(Character, Object)
	if Character and Object then
		local RightUpperLeg = Character:WaitForChild("RightUpperLeg")
		local PrimaryPart = Object.PrimaryPart
		PrimaryPart.Position = RightUpperLeg.Position
		local Weld = Instance.new("Weld")
		
		Weld.C0 = RightUpperLeg.CFrame:ToObjectSpace(PrimaryPart.CFrame + Vector3.new((RightUpperLeg.Size.X / 2) + 0.02, 0, -0.2))

		Weld.Part0 = RightUpperLeg
		Weld.Part1 = PrimaryPart
		Weld.Parent = PrimaryPart
		Object.Parent = Character
	end
end

Any help is appreciated. I can’t use the accessory instance because I’m trying to make the leg object fit on any character regardless of their size/bundle. (If I use accessory, certain packages like the woman package will make the leg object float in the air.)

Try to use WeldConstraint instead, and define de CFrame AFTER defining the parts.

3 Likes

The issue isn’t the welds, the issue is this part right here:

Weld.C0 = RightUpperLeg.CFrame:ToObjectSpace(PrimaryPart.CFrame + Vector3.new((RightUpperLeg.Size.X / 2) + 0.02, 0, -0.2))

I don’t know how to add in like an offset or something to make sure it’s always on the correct side of the leg.

WeldConstraints are much more easier and simpler to use. If the thing your welding is a model, make sure to weld all it’s descendants to the model’s primary part. That way when you weld the primary part to the leg, everything else follows.

CFrame is relative to the part, while Position is not. This is why your script only works in certain directions. In CFrame, for example, the X axis is always to the right and left of the character. In position there is a fixed X axis.

local RightUpperLeg = Character:WaitForChild("RightUpperLeg")

Object:SetPrimaryPartCFrame(RightUpperLeg.CFrame * CFrame.new(RightUpperLeg.Size.X/2, 0, 0) * CFrame.Angles(0, math.rad(90), 0))
local Weld = Instance.new("WeldConstraint")
Weld.Part0 = Object.PrimaryPart
Weld.Part1 = RightUpperLeg
Weld.Part = Object.PrimaryPart

The rotation and/or the position may be off depending on the axis’ and directions.

1 Like

f9dfcf02020fd5fbe2dccf78507356bf

My apologies, I messed up something on my end and left over some old extra weld stuff.

I got it to be correctly attach on all directions, except now it’s rotated oddly.

So you can put a leg in, add an attachment to the leg and move it to where your items root should be and copy the relative position. It’s Position is defined in the parts object space which is easy enough to replicate.

1 Like

I was thinking of doing something like this, but it wouldn’t work for all body types. (woman package, korblox leg, etc)

Try messing with

Maybe change the degree, or put it on another axis like:

CFrame.Angles(math.rad(90), 0, 0))

It depends on how your model is structured. It might not even need rotation at all.

1 Like

Well certainly you can need scaling along the relevant axis if you have varied bodies.
The main aspect is visualizing the position and getting a number from it relative to your body part. It makes a great start.

1 Like

I have tons of different models that are ‘objects’ that can be attached to the leg.

Does this mean I need to create them all along the same axis/orientation so it’ll be positioned correctly?

Thanks for the help, if Fusion’s solution doesn’t work out (it looks like it will so far) I’ll look into attachments further.

If you click on your object in studio, make sure the parts basically all face the same direction. If the front view of the object is along the Z axis, but the other object’s front direction is along the X axis it won’t work properly.

1 Like

Ah I figured. I found out the needed angles and it works perfectly now, in all directions. Thanks for the help :slight_smile:

1 Like