Below is one of the simplest, non-hacky ways to do it:
-
Every frame we measure the zoom-distance of the player’s camera.
When that distance is almost 0 studs, we know the player is in first-person.
(The zoom distance is just the distance between CurrentCamera.CFrame.Position
and CurrentCamera.Focus.Position.)
-
If the player is in first-person while shift-lock is ON, we turn shift-lock
OFF again (returning everything – camera offset, MouseBehaviour, icon, etc. –
to its default state).
Add the highlighted code (⚡) to your script:
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local CurrentCamera = workspace.CurrentCamera
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local mouseLockLabel = script.Parent
local mouseLock = nil -- heartbeat connection when SL is on
local SHIFT_LOCK_OFF = 'rbxasset://textures/ui/mouseLock_off.png'
local SHIFT_LOCK_ON = 'rbxasset://textures/ui/mouseLock_on.png'
local SHIFT_LOCK_CURSOR= 'rbxasset://textures/MouseLockedCursor.png'
local MaxLength = 900000
local EnabledOffset = CFrame.new(1.7, 0, 0)
local DisabledOffset = CFrame.new(-1.7, 0, 0)
--------------------------------------------------------------------
-- ⚡ Helper: returns true when the player has zoomed into 1st person
--------------------------------------------------------------------
local FIRST_PERSON_THRESHOLD = 0.6 -- studs
local function inFirstPerson()
return (CurrentCamera.CFrame.Position - CurrentCamera.Focus.Position).Magnitude
<= FIRST_PERSON_THRESHOLD
end
local function shiftLock(active)
local Character = Player.Character
if not Character or not Character:FindFirstChild("Humanoid") then return end
local hum = Character.Humanoid
local root = hum.RootPart
if active then
hum.CameraOffset = Vector3.new(1.75,0,0)
hum.AutoRotate = false
RunService:BindToRenderStep("ShiftLock", Enum.RenderPriority.Character.Value, function()
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
local _, y = CurrentCamera.CFrame.Rotation:ToEulerAnglesYXZ()
root.CFrame = CFrame.new(root.Position) * CFrame.Angles(0, y, 0)
end)
Mouse.Icon = SHIFT_LOCK_CURSOR
mouseLockLabel.Image = SHIFT_LOCK_ON
else
hum.CameraOffset = Vector3.new(0,0,0)
RunService:UnbindFromRenderStep("ShiftLock")
UserInputService.MouseBehavior = Enum.MouseBehavior.Default
hum.AutoRotate = true
Mouse.Icon = "http://www.roblox.com/asset/?id=7025168168"
mouseLockLabel.Image = SHIFT_LOCK_OFF
end
end
--------------------------------------------------------------------
-- ⚡ Small wrapper that really disables SL everywhere in the script
--------------------------------------------------------------------
local function disableShiftLock()
if mouseLock then
mouseLock:Disconnect()
mouseLock = nil
shiftLock(false)
CurrentCamera.CFrame = CurrentCamera.CFrame * DisabledOffset
end
end
local function shiftLockSwitcher()
if mouseLock then
disableShiftLock()
else
----------------------------------------------------------------
-- Shift-lock ON – start heartbeat loop that also checks 1st person
----------------------------------------------------------------
mouseLock = RunService.Heartbeat:Connect(function()
-- ⚡ Auto-disable when player zooms to first-person
if inFirstPerson() then
disableShiftLock()
return
end
-- (Existing SL code)
local Character = Player.Character
if Character and Character:FindFirstChild("Humanoid") and Character:FindFirstChild("HumanoidRootPart") then
local hum = Character.Humanoid
local root = Character.HumanoidRootPart
hum.AutoRotate = false
root.CFrame = CFrame.new(
root.Position,
Vector3.new(
CurrentCamera.CFrame.LookVector.X * MaxLength,
root.Position.Y,
CurrentCamera.CFrame.LookVector.Z * MaxLength
)
)
CurrentCamera.CFrame = CurrentCamera.CFrame * EnabledOffset
CurrentCamera.Focus = CFrame.fromMatrix(
CurrentCamera.Focus.Position,
CurrentCamera.CFrame.RightVector,
CurrentCamera.CFrame.UpVector
) * EnabledOffset
end
end)
shiftLock(true)
end
end
--------------------------------------------------------------------
-- UI / input bindings
--------------------------------------------------------------------
mouseLockLabel.MouseButton1Click:Connect(shiftLockSwitcher)
UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
shiftLockSwitcher()
end
end)