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

What have I done wrong?

function part(pos1, pos2)
 	local p = Instance.new("Part", Workspace)
 	p.Anchored = true
 	local mag = (pos2 - pos1).magnitude
 	p.Size = Vector3.new(1, mag, 1)
 	p.CFrame = (CFrame.new(pos1, pos2)) * CFrame.new(0, p.Size.Y / 2, 0)
 	return p
end

That code up there is supposed to create a part that stretches from one position to another, with one end on pos1, and the other end on pos2, It doesn't error, but for some reason, it starts at pos1, but doesn't end in pos2, it inverts it kind of, it's hard to explain. Does anybody know how i can fix that?

1 answer

✓ Accepted answer

Your line setting the CFrame is off. The two CFrames being multiplied are both close to right, but both not right.


CFrame.new(pos1, pos2) should be CFrame.new( (pos1 + pos2) / 2, pos2).

This is because the 2-argument CFrame.new takes a (center, lookingToward).

Since pos1 is actually one end, and not the center, you need to make the center.

The center is the average of the two ends, which is (pos1 + pos2) / 2.


So, the above will create a part facing at pos2. But since you specified the part's height, this will be facing up and down. We just have to multiply this by 90° -- it's not based on the height of the part:

The second CFrame should be CFrame.Angles( math.rad(-90) , 0 , 0 ).


Here is what that final line should look like:

p.CFrame = CFrame.new( (pos1 + pos2) / 2, pos2)
		 * CFrame.Angles( math.rad(-90) , 0 , 0 )


ERRATA: Original version of answer incorrectly used math.deg instead of math.rad. This was changed within a minute or two

1 comment

0

Thank you sir!

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