Script weirdly rotates my character

So I have this script and its supposed to rotate my player according to the slope and it does that but the problem is that when im not on a slope or something it weird rotates my character


image

connection = game:GetService("RunService").Heartbeat:Connect(function()
	local r = Ray.new(root.Position, Vector3.new(0,-(humanoid.HipHeight + 4),0))
	local hit,pos,normal = workspace:FindPartOnRayWithIgnoreList(r,{character})
	local vector = (root.CFrame - root.CFrame.p):VectorToObjectSpace(normal)
	if vector.z > 0 then
		root.RootJoint.C0 = CFrame.new(0,vector.z * 1.25,0,-1,0,0,0,0,1,0,1,0) * CFrame.Angles(-vector.z,0,-vector.x)
	else
		root.RootJoint.C0 = CFrame.new(0,-vector.z * 1.25,0,-1,0,0,0,0,1,0,1,0) * CFrame.Angles(-vector.z,0,-vector.x)
	end
end)

I’m 99% sure creating that many rays is unnecessary and extremely heavy on the client. Next, if you changed -vector.z to 0 would that fix the issue?

As @wupf said it is heavy on the client. If you 100% have to you should add a .1 or .05 wait between just to tone down the lag a bit.