As the title says: How would you create a weld between two parts, using a script?
This function should do the trick:
local function WeldParts(Part0, Part1)
if Part0:IsA("BasePart") and Part1:IsA("BasePart") then
local Weld = Instance.new("Weld")
Weld.Part0 = Part0
Weld.Part1 = Part1
Weld.C0 = Part0.CFrame:inverse()
Weld.C1 = Part1.CFrame:inverse()
Weld.Parent = Part0
Part0.Anchored = false
Part1.Anchored = false
end
end
EDIT: You can also use WeldConstraints (which do not involve setting the C0 and C1). See @BenMactavsin’s reply for more details on that. ![]()
is it ok if you can just give me a quick rundown of how that all works and what the Weld.C stuff mean?
You can use a WeldConstaint instead of a Weld which is much simpler to use than a normal Weld:
local Part0 = --Reference to BasePart here.
local Part1 = --Reference to other BasePart here.
local Weld = Instance.new("WeldConstraint")
Weld.Part0 = Part0
Weld.Part1 = Part1
Weld.Parent = --You can referece to any object you want here.
Sure. Sorry, I should have done that in the first place.
You can pass two parts into the function as parameters. The script will check if they are both BaseParts (the main part class, includes unions, normal parts, meshparts, etc).
Then, it creates a weld and sets the Part0 and Part1 parameters to the corresponding parts, and then uses the part’s CFrame inverses to set the weld through the C0 and C1 properties. These properties basically tell the weld where each part should be positioned.
I’m not very good at explaining, so I encourage you to read more about how welds work on the Developer Hub:
Edit: I also didn’t think about WeldConstraints, they would likely be better for your usecase.
If you are using the old weld system which uses a Joint Instance like C0 and C1 a good way to visualize it is using RigEdit and Motor6D’s as it uses the same system.
Here is a diagram of how it looks like when you create a weld/Motor6d between two parts:
Part0: Hip
Part 1: Upper leg
TL;DR your best bet to learn it is by experimenting setting the C0 joints to an empty CFrame value, using CFrame.new() to make a part look at something and yeah.
