Make head moving script stop at certain point?

So I got this script that allows an npc to look at the player, but it turns in every direction, how do i make it so if they look to far left or right, it doesn’t go anymore?

local neck = script.Parent.Head.Neck
local NPC = script.Parent

function getClosestPlayer()
	local closest_player, closest_distance = nil, 30
	for i, player in pairs(workspace:GetChildren()) do
		if player:FindFirstChild("Humanoid") and player ~= NPC then
			local distance = (NPC.PrimaryPart.Position - player.PrimaryPart.Position).Magnitude
			if distance < closest_distance then
				closest_player = player
				closest_distance = distance
			end
		end
	end
	return closest_player
end

local cframe0 = neck.C0
while true do
	local player = getClosestPlayer()
	if player then
		local is_in_front = NPC.PrimaryPart.CFrame:ToObjectSpace(player.PrimaryPart.CFrame).Z < 0
		if is_in_front then
			local unit = -(NPC.PrimaryPart.CFrame.p - player.PrimaryPart.Position).unit
			neck.C0 = cframe0 * CFrame.new(Vector3.new(0, 0, 0), unit) * CFrame.Angles(0, -math.rad(NPC.PrimaryPart.Orientation.Y), 0)
		end
	end
	wait()
end

Its usually easiest to do these types of things with angles, because they’re easily adjustable and such

I would do something like:

--loop begins here
local rel = npc.PrimaryPart.CFrame:Inverse()*player.PrimaryPart.CFrame
--rel is player converted to object space of npc
local up = math.atan2(rel.Y,-rel.Z)
local side = math.atan2(-rel.Z,rel.X)

up = math.clamp(up,lower,upper) --clamp it to radian value here
side = math.clamp(up,lower,upper)

neck.C0 = cframe0*CFrame.Angles(up,side,0)
--loop ends here