PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Weld Joints

Weld Joints

Oct 15 2018, 1:40 AM PST 5 min

Welds are a type of JointInstance that rigidly holds their parent part and another part in a fixed position relative to each other, such as holding one part 2 units to the right of another part. They can be used in places where parts need to be held together at odd angles, but still be able to move (meaning they will not work with BasePart/Anchored parts), such as for a vehicle.

Prerequisites

  • Being able to use a basic Articles/Understanding CFrame (CFrame.new(#, #, #)).
  • Knowing how to index and change the properties of an object, using a script.

Basic structure of a weld

The weld object is placed inside of a part, and a property is set to determine which other parts should be welded to the original part. Then two CFrames, the C0, and the C1, tell the weld how the parts should be placed.

Weld-Table.png

Advanced

part1.CFrame * C1 == Part0.CFrame * C0

Since C0 and C1 are both offsets this equation means that the weld must satisfy the condition that each part CFrame multiplied by its respective offset must end up being equal to each other. This looks like this:

Weld_equality.gif

  • Where blue is the C0 offset and red is C1 offset.

Setting the values

Figuring out what to set the C0 and C1 to is a bit finicky, but once you get good at it, it can go quite quickly.
For Welds, you don’t have to worry about the C1, it’s automatically set to a “unit” or unrotated CFrame, you only have to deal with C1 when working with motors, so ignore it for now. The C0 will tell the weld how it should attach itself to the other part, eg.

weld.C0 = CFrame.new(0, 2, 0) 

This tells the weld that it should hold the part1 in a position 2 studs above the part0, simple eh?

Rotating the CFrame

To rotate the CFrame you must use the following command, which is case sensitive.

CFrame.fromEulerAnglesXYZ(#, #, #) 

Or, alternatively:

CFrame.Angles(#, #, #)

This is used as follows:

weld.C0 = CFrame.new(0, 2, 0)*CFrame.fromEulerAnglesXYZ(0, math.pi, 0)

This tells the weld to attach the part1, two studs above the part0 AND rotate the part1 by 180 degrees relative to the part0

You use math.pi, as the number to rotate by, use it in the following fashion :

  • math.pi = 1/2 of a turn
  • math.pi/2 = 1/4 of a turn
  • math.pi/4 = 1/8 of a turn

Putting it all together

Now that you know how to use the basics of a weld, here’s how you put it all together. Here’s an example:

Let’s say that this is in a script, in a vehicle:

local pln = script.Parent 

local w = Instance.new("ManualWeld") 
w.Parent = pln
w.Part0 = pln.Engine
w.Part1 = pln.Wing1 
w.C0 = CFrame.new(0, 0, 6)*CFrame.new(0, -math.pi/5, 0)

Walkthrough

  • Open Roblox Studio
  • Insert > Object > Part
  • Rename Part to “Engine”
  • Insert > Object > Part
  • Rename Part to “Wing1”
  • Group Engine and Wing1 together as “pln”
  • Click “pln”
  • Insert > Object > into “pln”
  • Copy and paste the script in “Putting it all together” above.
  • Test… Engine should have a “weld” logo in the Explorer menu.

Welding together two existing bricks

Sometimes, you want to just weld together two parts in their current positions, so that they remain in the same relative positions. This takes just a basic understanding of CFrame math and welds. As previously mentioned,

Part1.CFrame * C1 == Part0.CFrame * C0

And understanding that

CFrameA:inverse() * CFrameA

cancels out, we can apply some basic algebra

Part0.CFrame * C0 == Part1.CFrame * C1
Part0.CFrame:inverse() * Part0.CFrame * C0 == Part0.CFrame:inverse() * Part1.CFrame * C1
C0 == Part0.CFrame:inverse() * Part1.CFrame * C1

So this means we can do :

local function weldBetween(a, b)
	--Make a new Weld and Parent it to a.
	local weld = Instance.new("ManualWeld", a)
	--Get the CFrame of b relative to a.
	weld.C0 = a.CFrame:inverse() * b.CFrame
	--Set the Part0 and Part1 properties respectively
	weld.Part0 = a
	weld.Part1 = b
	--Return the reference to the weld so that you can change it later.
	return weld
end

You can then do the following to weld two parts together:

local weld = weldBetween(game.Workspace.Part, game.Workspace.Brick)

See Also

  • Weld class documentation
  • WeldConstraint, for a newer alternative using the Articles/Constraints|constraints system
  • ManualWeld
  • Articles/Understanding CFrame
Tags:
  • constraints
  • building