Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is my welding script welding with incorrect CFrame?

I am in a desperate need of help.

I am trying to make a simple welding script, but my parts always weld with the incorrect position and orientation. I think it has something to do with weld.C0, so I have made many attempts to change the weld.C0 in hopes of getting it right, but I am having no luck.

Handi = script.Parent:FindFirstChild('Handi')
Parts = Handi:GetChildren()
for i = 1, #Parts do
	if Parts[i]:IsA('BasePart') then
		local weld = Instance.new("Weld")
		weld.Part0 = Handi
		weld.Part1 = Parts[i]
		-- Here is where I make changes. I think this is where I am having problems.
		weld.C0 = Handi.CFrame:inverse() * Handi.CFrame
		weld.C1 = Parts[i].CFrame:inverse() * Handi.CFrame
		weld.Parent = Handi
	end
end

I have tried video tutorials, development posts, blog posts, development forums, discord servers, and I have even reached out to some fellow scripting friends. I am having no luck.

3 comments

0

C0 is just the offset from Part0. weld.C0 = Parts[i].CFrame * Handi.CFrame:Inverse() would set the offset to Part1's CFrame, then subtracting that by Part0's CFrame. That makes an offset of the distance between the two parts.

0

You don't need C1

0

I tried that, but its still not working for me :(

1 answer

✓ Accepted answer

I'll put you in the right direction but since you haven't provided how you want the weld to be offset I can't give you the final result

For what we are doing, let's not use C1 of the weld since we can do mostly everything with C0

Without setting C0 the weld connections the two parts together middle to middle, so how can we use C0 to offset our part?

The first thing we do is Create a CFrame with the offset in studs relative to Part0, remember that the x,y,z you provide will move it REALATIVE, to Part0, so if the part is rotated a bit, x will make it move right or left TO the part no matter how it is orientated.

so for example

C0=CFrame.new(5,5,0)

Will offset Part1 by 5 to the right, and 5 up relative to Part0

Next we will changes its orientation using CFrame.Angles() CFrame.Angles uses something called radians which you will learn in trigonometry. You may be more known with the term degrees instead, roblox gives us a way to easily change degrees to radians without us having to math using math.rad(degrees)

We use it like this

-- first we offset then we rotate
C0=CFrame.new(5,5,0) * CFrame.Angles(0, math.rad(90), 0)

This will rotate it on its Y axis by 90 degrees

Heres our example in action.

Handi = script.Parent:FindFirstChild('Handi')
Parts = Handi:GetChildren()
for i = 1, #Parts do
    if Parts[i]:IsA('BasePart') then
        local weld = Instance.new("Weld")
        weld.Part0 = Handi
        weld.Part1 = Parts[i]
        
        
        C0=CFrame.new(5,5,0) * CFrame.Angles(0, math.rad(90), 0)

        weld.Parent = Handi
    end
end

At the end of the day, you will have to iterate over and over again by changing these values and running to see the result.

Sooner or later you'll imagine how it'll turn out before you run it and you'll be able to offset quicker.

Till then you will need to experiment with these numbers and change them till you reach your desired result.

Good Luck!

1 comment

0

Thanks, this was very helpful reading :)

This discussion is preserved as it was. New answers, comments, and votes are closed.