CFrame.lookat is having some... 'minor' difficulties

So im trying to make the cframe.lookat work with welds but its doing some funny stuff. Not sure but i dont think this is how a tank functions.


while task.wait() do
	script.Parent.Parent.TurretBody.Barrel.C1 *= CFrame.lookAt(script.Parent:GetPivot().Position, Vector3.new(script.Parent:GetPivot().Position.X, workspace.Part.Position.Y, workspace.Part.Position.Z)) * CFrame.Angles(0,math.pi,0)
end|

Here’s an updated version of your code can fixes the issue

while true do
    local pivotPosition = script.Parent:GetPivot().Position
    local targetPosition = workspace.Part.Position
    
    local lookVector = (targetPosition - pivotPosition).Unit
    local upVector = Vector3.new(0, 1, 0) -- Specify the desired up vector here
    
    local lookAtCFrame = CFrame.lookAt(pivotPosition, pivotPosition + lookVector, upVector)
    
    script.Parent.Parent.TurretBody.Barrel.C1 = lookAtCFrame
    
    task.wait()
end

Nope, didn’t work. The barrel just flies around

Try This

while true do
    local pivotPosition = script.Parent:GetPivot().Position
    local targetPosition = workspace.Part.Position
    
    -- Adjust targetPosition to a safe distance away from other objects
    
    -- Calculate the direction vector and distance to the target
    local direction = targetPosition - pivotPosition
    local distance = direction.Magnitude
    
    -- Specify a maximum distance to prevent rushing into objects
    local maxDistance = 5
    
    -- Check if the distance exceeds the maximum
    if distance > maxDistance then
        targetPosition = pivotPosition + direction.Unit * maxDistance
    end
    
    local lookVector = (targetPosition - pivotPosition).Unit
    local upVector = Vector3.new(0, 1, 0) -- Specify the desired up vector here
    
    local lookAtCFrame = CFrame.lookAt(pivotPosition, pivotPosition + lookVector, upVector)
    
    script.Parent.Parent.TurretBody.Barrel.C1 = lookAtCFrame
    
    task.wait()
end

Nope, also doesnt work. I had this but it also didn’t work.

	local transform = CFrame.lookAt(script.Parent:GetPivot().Position, Vector3.new(script.Parent:GetPivot().Position.X, workspace.Part.Position.Y, workspace.Part.Position.Z))
	script.Parent.Parent.TurretBody.Barrel.C1 = (script.Parent.Parent.TurretBody.Barrel.Part1.CFrame:Inverse() * script.Parent.Parent.TurretBody.Barrel.Part0.CFrame) * transform```
local Barrel =script.Parent.Parent.TurretBody.Barrel 
while true do
task.wait()
	Barrel.C1 = Barrel.Part1.CFrame:ToObjectSpace(CFrame.lookAt(Barrel.Part0.CFrame.Position, workspace.Part.Position))
end

I’m on mobile so you’ll need to add your own edits.

Nope, it glitches out: