Unable to line up angles to make a laser gun's laser work properly near the axis points

I have to the best of my abilities tried to create a laser gun, yet I have only partially succeeded, I managed to get a laser to track around all 4 quadrants of a graph(as in the ++ -+ + - parts of a coordinate graph), but whenever I aim near the axes(plural of Axis), the laser doesn’t move as intended(which is to be angled from the gun’s barrel to the point where the mouse hit an object).
This is my code:

repeat wait() until #script.Parent:GetChildren() == 9
local ClickedRemote = script.Parent.Clicked
local Laser, Laser2, Laser3 = script.Parent.Laser, script.Parent.Laser2, script.Parent.Laser3
local Handle = script.Parent.Handle.ShootPart
ClickedRemote.OnServerEvent:Connect(function(plr, Hit)
	script.Parent.Player.Text = tostring(plr)
	local Part = script.Parent.MeltPart:Clone()
	Part.Size = Vector3.new(.5,.5,.5)
	Part.Position = Vector3.new(Hit.X, Hit.Y--[[(Part.Size.Y/2)]], Hit.Z)
	Part.Color = Color3.fromRGB(255, 140, 0)
	Part.Material = Enum.Material.Neon
	Part.Anchored = true
	Part.Script.Enabled = true
	Part.Parent = game.Workspace.Parts
	local AP = Vector3.new((Part.Position.X+Handle.Position.X)/2, (Part.Position.Y+Handle.Position.Y)/2, (Part.Position.Z+Handle.Position.Z)/2)
	local X = math.sqrt((Part.Position.X-Handle.Position.X)^2+(Part.Position.Y-Handle.Position.Y)^2+(Part.Position.Z-Handle.Position.Z)^2)
	local S = Vector3.new(X, Part.Size.Y, Part.Size.Z)
	Laser.Position = AP
	Laser.Size = S
	local RX, RY, RZ = math.deg(math.acos((math.sqrt((Part.Position.Y-Handle.Position.Y)^2)/X))), 0, ((Part.Position.Z-Handle.Position.Z)/(Part.Position.Z-Handle.Position.Z))*((math.deg(math.acos((math.sqrt((Part.Position.Z-Handle.Position.Z)^2)/X)))+90-((math.deg(math.acos((math.sqrt((Part.Position.Z-Handle.Position.Z)^2)/X)))+180)*2)))
	if Part.Position.Z > 0 then
		RX, RY, RZ = -RX, -RY, -RZ
		if Part.Position.X < 0 and Part.Position.Z < 0 then
			RY = RY-270
		elseif Part.Position.X < 0 then
			RY = RY-180
		end
	else
		if Part.Position.X > 0 and Part.Position.Z > 0 then
			RY = RY-270
		elseif Part.Position.X > 0 then
			RY = RY-180
		end
	end
	Laser.CFrame = CFrame.new(AP)*CFrame.Angles(math.rad(RX), math.rad(RY), math.rad(RZ))
end)
2 Likes