Hinge target angle inverting for some reason / not going beyond 90 degrees

for some reason the hinge wont go past 90 degrees. I don’t know how to fix it. It is probably with the mouse and not the hinge.


code:

-- Wait for player to join game
repeat wait() until #game.Players:GetChildren() > 0
Player = game.Players:GetChildren()[1]

-- Wait for Player's character to load
if not Player.Character then
	Player.CharacterAdded:Wait()
end

-- define some variables
Char = Player.Character
Hinge = script.Parent
RootPart = Hinge.Parent
event = game:GetService("ReplicatedStorage").Events.GEEE

event.OnServerEvent:Connect(function(plr,lr)
	-- define position of origin
	local HKpos = Vector2.new(RootPart.Position.X,RootPart.Position.Z)
	-- define position of target
	local XZpos = Vector2.new(lr.Position.X,lr.Position.Z)

	-- the ratio to get the triangle's width over the height
	local ratio = (XZpos.Y-HKpos.Y)/(XZpos.X-HKpos.X)
	local beta = math.atan(ratio) -- converting this to radians
	
	-- turn it from radians into degrees
	beta = beta*(180/math.pi)
	
	print(beta)
	Hinge.TargetAngle = -beta -- beta has to be negative so it points at you
end)
--on the client it is sending the mouse.hit.position

(THIS IS NOT MY MODEL)
i modified it to point where the mouse is and not where the payer is. here is the model if you want it:
https://web.roblox.com/library/4506974149/Servo-that-points-at-you


i want to uninvert (if thats a word) the red section. how would i do that?
hope this helps

IIRC this issue is because math.atan doesn’t account for going past into the quadrants with a negative Y coordinate.

Use math.atan2 which accounts for going past 180 degrees or “into quadrants with a negative Y coordinate”. math.atan2(Y, X); you pass the opp and adj as parameters and it will do the division depending on the value of the parameters. You might also want to account for the case of x and y being 0, in which case you use math.atan2(Y, X) or 0

image
For a more elaborated answer try this stack overflow question: