# How to lock a rotation value

im trying to make it so a turret stops following the mouse at a certain point
when it does it spins

code:

``````aimpos = mouse.Hit.p
if gun.gun.base.Orientation.y >= 105 and dontmove == false then
gun.gun.base.Orientation = Vector3.new(gun.gun.base.Orientation.x,104,gun.gun.base.Orientation.z)
dontmove = true
end
if gun.gun.base.Orientation.y <= 77 and dontmove == false then
gun.gun.base.Orientation = Vector3.new(gun.gun.base.Orientation.x,78,gun.gun.base.Orientation.z)
dontmove = true
end
if gun.gun.base.Orientation.y <= 104 and gun.gun.base.Orientation.y >= 78 and dontmove == true then
dontmove = false
end
if (aimpos-gun.base.Position).magnitude>3 and dontmove == false then

local c = aimpos
local x = (gun.gun:GetPrimaryPartCFrame()*CFrame.new(0, 0, -20)).x
local y = SPAWN.y
local z = (gun.gun:GetPrimaryPartCFrame()*CFrame.new(0, 0, -20)).z
A = Vector3.new(x,
y,
z
)
if c then
gun.gun:SetPrimaryPartCFrame(
CFrame.new(gun.body:GetPrimaryPartCFrame().p,Vector3.new(c.x, -20, c.z))
)
gun.body:SetPrimaryPartCFrame(
CFrame.new(
SPAWN.p,
A
)
)
else
gun:SetPrimaryPartCFrame(gun.body:GetPrimaryPartCFrame())
gun.body:SetPrimaryPartCFrame(
CFrame.new(
SPAWN.p,
A
)
)
end
end``````

You can use math.clamp() to limit a value between a min and max. You can also just try using a hingeconstraint and set the min and max rotation angles

i got it to work with left and right but now when i try to make it work with going down it spins like crazy

code:

``````mouse.Move:Connect(function()
local delta = mouse.Hit.Position - gun.gun.base.CFrame.Position
local y = mouse.Hit.p.y

gun.gun.base.HingeConstraint.TargetAngle = math.deg(math.atan2(delta.Z, delta.X))
gun.gun.base.Orientation = Vector3.new(gun.gun.base.Orientation.x,y,gun.gun.base.Orientation.z)
end)``````