How to make NPC face limited direction?

So i have this script that makes the camera move slightly and is supposed to turn the character in the direction of the mouse, but i dont want to be able to do a 360, i tried making it detect orientation but breaks the script with no errors.

local maxTilt = 10
game:GetService("RunService").RenderStepped:Connect(function()
	if customcam == false then
		if Root.Parent.Orientation.Y <= -120.868 and Root.Parent.Orientation.Y >= 132.37
		local RootPos, MousePos = Root.Position, Mouse.Hit.Position
		Root.CFrame = CFrame.new(RootPos, Vector3.new(MousePos.X, RootPos.Y, MousePos.Z))
		currentCamera.CFrame = game.Workspace.Cameras:WaitForChild("CameraPart").CFrame * CFrame.Angles(math.rad((((mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY)) * -maxTilt),math.rad((((mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX)) * -maxTilt),0)
		end
end)

(i can provide video for better insite)

1 Like
if Root.Parent.Orientation.Y <= -120.868 and Root.Parent.Orientation.Y >= 132.37

It is checking if the part orientation is lower than -120.868, which if it is, it passes to the second argument, and the rest of the code will only run if it is higher than 132.37

The problem is the logic, if the first argument is true, the second is false, thus it will not run, however, if the second is true, the first is false, thus it will also not run

What you should do is probably use math.clamp on the line

currentCamera.CFrame = game.Workspace.Cameras:WaitForChild("CameraPart").CFrame * CFrame.Angles(math.rad((((mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY)) * -maxTilt),math.rad((((mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX)) * -maxTilt),0)

to instead, for an example

currentCamera.CFrame = game.Workspace.Cameras:WaitForChild("CameraPart").CFrame * CFrame.Angles(math.rad((((mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY)) * -maxTilt),math.clamp(math.rad((((mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX)) * -maxTilt),-math.rad(130),math.rad(130)),0)