Weld function behaving strangely

I have this pretty standard weld function (see below) that welds 2 parts together keeping the original offset of the parts. Here is the strange part: If I feed the function the optional parameter c1 with the EXACT same code otherwise run within the function

part1.CFrame:ToObjectSpace(part0.CFrame)

It will work as intended, if I leave the c1 parameter blank I suddenly get a completely different result for c1 and the one part is simply welded to the center of the other despite the code being the same. I’m really confused by this behavior.

local function WeldTogether(part0: BasePart, part1: BasePart, c1: CFrame)`
    
    	local weld = Instance.new("Motor6D", part0)
    	weld.Part0 = part0
    	weld.Part1 = part1
    	weld.C1 = c1 or part1.CFrame:ToObjectSpace(part0.CFrame)
    	weld.Parent = part0

    	return weld
    end

    return WeldTogether

You are providing the parent parameter to Instance new, then immediately setting Part0 and Part1. It’s at this point where Roblox may be moving your parts before you’ve set C1. This is problematic because the location of the parts matters on the line where you set C1.

1 Like

I can’t believe I missed that :clown_face:

BTW I remember your mini roller coaster builder game from back in 2010
Thanks!

1 Like