Rotating a body based on Difference in leg height

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to rotate a body by finding the difference in height of its legs

  2. What is the issue? Include screenshots / videos if possible!
    I am finding the average of its front leg and back legs and finding the difference and it doesnt work and just keeps on rotating.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    yes i did, the solution i used is in the code given below. i just made it look at the target and move that way but it comes with a big flaw

So i want help on rotating a body by calculating the difference in its leg heights and using that

while true do
	local l1y = workspace.P2.Position.Y
	local l2y = workspace.P4.Position.Y
	local l3y = workspace.P6.Position.Y
	local l4y = workspace.P8.Position.Y

	local avgy = (l1y + l2y + l3y + l4y)/4 + 7
	local favg = (l1y + l3y)/2
	local bavg = (l2y + l4y)/2

	local lavg = (l1y + l2y)/2
	local ravg = (l3y + l4y)/2

	local rx = favg - bavg
	local rz = lavg - ravg
	
	--script.Parent.Rotation = Vector3.new(rx*2.2,0,0)

	script.Parent.CFrame = CFrame.new(script.Parent.Position, workspace.h.Position)

	script.Parent.Position += script.Parent.CFrame.LookVector * SPEED
	workspace.P1.Position += script.Parent.CFrame.LookVector * SPEED
	workspace.P3.Position += script.Parent.CFrame.LookVector * SPEED
	workspace.P5.Position += script.Parent.CFrame.LookVector * SPEED
	workspace.P7.Position += script.Parent.CFrame.LookVector * SPEED

	script.Parent.Position = Vector3.new(script.Parent.Position.X,avgy,script.Parent.Position.Z)
	workspace.P1.Position = Vector3.new(workspace.P1.Position.X,avgy,workspace.P1.Position.Z)
	workspace.P3.Position = Vector3.new(workspace.P3.Position.X,avgy,workspace.P3.Position.Z)
	workspace.P5.Position = Vector3.new(workspace.P5.Position.X,avgy,workspace.P5.Position.Z)
	workspace.P7.Position = Vector3.new(workspace.P7.Position.X,avgy,workspace.P7.Position.Z)

	wait()
end

this is the error

script.Parent.CFrame *= CFrame.Angles(Math.rad(rx)*2, 0, 0)

it causes the thing to keep spinning, i want it to be a fixed rotation
yeah i know it contains multiply but i do not know of any other way of setting the rotation without making its position zero

Have you found any solutions?
I have this exact same problems and I dont know how to fix it.
I’ve only found this reddit post of someone getting it but it’s for unity but I don’t know how to translate it:

using System.Linq; 

void ControlRotation()
{
    GetAverages(out var right, out var left);
    var rotation = Quaternion.LookDirection(right - left);
}

void GetAverages(out Vector3 right, out Vector3 left)
{
    //      Vector3[] with all Legs on the right side
    right = rightLegs.Aggregate(Vector3.zero, (current, l) => current + l);
    //      Vector3[] with all Legs on the left side
    left = leftLegs.Aggregate(Vector3.zero, (current, l) => current + l);
    right /= (float) rightLegs.Length;
    left /= (float) leftLegs.Length;
}