Part moving to extremely far away positions when using CFrame.lookAt

Hello!

Basically, I am working on a roof generation algorithm which should create a gable roof, regardless of rotation. That’s what I am having an issue with, but am not sure why. Basically, whenever I generate the roof, it doesn’t work, and one of the WedgeParts gets moved very far away (0, -340282346638528859811704183484516925440, 0). It works okay when trying to not use the second .lookAt, however when I do use the second lookAt, the parts get put at this position.

local partwidth0 = workspace.pw 
local partwidth1 = workspace.pw2
local height = 150
local heightnum = 150

local heightpart = Instance.new('Part', workspace)
heightpart.Position = (partwidth0.Position + partwidth1.Position) / 2
heightpart.Name = 'HEIGHT'
heightpart.Position = Vector3.new(heightpart.Position.X, heightpart.Position.Y + height, heightpart.Position.Z)
height = heightpart

local part = Instance.new('Part', workspace)
local length = (partwidth0.Position - heightpart.Position).Magnitude + 2*height.Position.Y*part.Size.Y/part.Size.Z
local dist = (partwidth0.Position - heightpart.Position).Magnitude + 2*height.Position.Y*part.Size.Y/(partwidth1.Position - partwidth0.Position).Magnitude

part.Position = (partwidth0.Position + height.Position) / 2
part.CFrame = CFrame.lookAt(part.Position, height.Position)
part.Size = Vector3.new(part.Size.X, part.Size.Y, dist)

local part2 = Instance.new('Part', workspace)
part2.Position = (partwidth1.Position + height.Position) / 2
part2.CFrame = CFrame.lookAt(part2.Position, height.Position)
part2.Size = Vector3.new(part2.Size.X, part2.Size.Y, dist)

local fill = Instance.new('WedgePart', workspace)
fill.Name = 'WEDGE_0'
local fillHeight = Instance.new('Part', workspace)
local fillWidth = Instance.new('Part', workspace)
local fillWidtharc = fillWidth
fillHeight.Position = Vector3.new(partwidth0.Position.X, heightpart.Position.Y, partwidth0.Position.Z)
fillWidth.Position = Vector3.new(heightpart.Position.X, partwidth0.Position.Y, heightpart.Position.Z)
local fillWidth = (Vector3.new(heightpart.Position.X, partwidth0.Position.Y, heightpart.Position.Z) - Vector3.new(partwidth0.Position)).Magnitude
local fillWidthNumerical = (Vector3.new(partwidth0.Position.X, heightpart.Position.Y, partwidth0.Position.Z) - heightpart.Position).Magnitude
fill.Size = Vector3.new(1, .5 +(heightpart.Position.Y - partwidth0.Position.Y), fillWidthNumerical)
fill.Position = part.Position

local fill2 = Instance.new('WedgePart', workspace)
fill2.Name = 'WEDGE_1'
local fillHeight = Instance.new('Part', workspace)
local fillWidth = Instance.new('Part', workspace)
local fillWidtharc = fillWidth
fillHeight.Position = Vector3.new(partwidth0.Position.X, heightpart.Position.Y, partwidth0.Position.Z)
fillWidth.Position = Vector3.new(heightpart.Position.X, partwidth0.Position.Y, heightpart.Position.Z)
local fillWidth = (Vector3.new(heightpart.Position.X, partwidth0.Position.Y, heightpart.Position.Z) - Vector3.new(partwidth0.Position)).Magnitude
local fillWidthNumerical = (Vector3.new(partwidth0.Position.X, heightpart.Position.Y, partwidth0.Position.Z) - heightpart.Position).Magnitude
fill2.Size = Vector3.new(1, .5 +(heightpart.Position.Y - partwidth0.Position.Y), fillWidthNumerical)
fill2.Position = part2.Position


fill.CFrame = CFrame.lookAt(fill2.Position, fill.Position) -- line causing problems
fill2.CFrame = CFrame.lookAt(fill.Position, fill2.Position) --line also causing problems

fillHeight:Destroy()
fillWidtharc:Destroy()
height:Destroy()
heightpart:Destroy()

I hate to bump but this issue is still persistent. I only really need help with determining why CFrame.lookAt is moving the part super far away.

In this line

fill.CFrame = CFrame.lookAt(fill2.Position, fill.Position)

you put fill to the same position as fill2. Then, on the next line, you set fill2’s position and lookAt to the positions of the parts. Because the positions are same, you give the same two vectors. This means that the new CFrame can’t be calculated correctly because no direction can be found.

Consider using CFrame.new(PartPosition, PositionToFace).

Isn’t that the exact same method with different name?
Edit: except that it doesn’t have an optional upvector parameter

Well, yes, this is just a minor suggestion to make the line less long.