I keep asking the same question, but having tried a lot of stuff today idk what to do. The x-rotation of the turret, which is a speerate par tof the turret that moves only on the x-axis, only aims correctly when the target is very close to it (> 5 studs away). Hoe would i make the turret aim correctly?

Variable hinge_xz is the working hinge, works perfectly

Variable hinge_xyz is the one that has the issue

Please, help! (I've been going at this for 4 days now :[ )

local Part = Instance.new('Part')
function getOrientation(cf)
	Part.CFrame = cf
	return Part.Orientation
end
wait(10)
while wait(firerate) do
	local target = nil
	
	for i, v in pairs(game.Workspace:GetChildren()) do
		if targetfolder1:FindFirstChild(v.Name) then
			if (v["MAIN"].Position - script.Parent.Hitbox.Position).magnitude < aggrodist then
				local dummypart = Instance.new("Part")
				target = v["MAIN"]
				script.Parent.Target.Value = v
				dummypart.Parent = workspace
				dummypart.Anchored = true
				local c_Frame = CFrame.new(script.Parent.FakeHinge.Position, target.Parent:GetPrimaryPartCFrame().p)
				dummypart.CFrame = c_Frame

				local X = getOrientation(c_Frame).X
				local y = dummypart.Orientation.Y
				hinge_xz.TargetAngle = y
				local vector_dy = (target.Position - script.Parent.FakeHinge.Position).magnitude
				print(vector_dy, "DY")
				local vector_dx = (Vector3.new(target.Position.X,  script.Parent.FakeHinge.Position.Y, target.Position.Z) - script.Parent.FakeHinge.Position).magnitude
				print(vector_dx, "DX")
				local finalx = math.deg(math.atan((vector_dx / vector_dy)))
				print(finalx)
				--print(finalx)
				hinge_xyz.TargetAngle  = finalx - 35
				dummypart:Destroy()
				--end
			else
				hinge_xz.TargetAngle = 0
			end
		end
	end
end