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. :slight_smile:

4 Likes