Force Shift Lock (Using Player Module)

Recently, I was looking for a way to force shift lock in Roblox. I tried several methods without using the player module, but none of them provided a smooth experience. After searching for methods involving the player module, I couldn’t find much information. However, I eventually devised a solution using a combination of approaches.

Steps to Force Shift Lock

  1. Modify the Camera Module:

    In the CameraModule, within the .new function, add the following line to initialize the custom lock:

    self.CustomLock = true
    

    Setting it to true ensures that players are forced into shift lock upon spawning.

  2. Update the Camera Controller:

    In the :Update function within the CameraModule, add this line of code to apply the custom lock setting:

    self.activeCameraController:SetIsMouseLocked(self.CustomLock)
    
  3. Create a ToggleShiftLock Function:

    Next, create a function within the CameraModule to toggle the shift lock:

    function CameraModule:ToggleShiftLock(Var)
        self.CustomLock = Var
    end
    
  4. Add a ForceLock Function to the Player Module:

    In the PlayerModule, create a function to force the shift lock:

    function PlayerModule:ForceLock(var)
        self.cameras:ToggleShiftLock(var)
    end
    

Example of Using the ForceLock Function

To call the ForceLock function, you can use the following code:

local playerModule = require(localPlayer:WaitForChild("PlayerScripts"):WaitForChild("PlayerModule")).new()
playerModule:ForceLock(true)

Additional Fixes

To address some issues that might arise, you can add these lines of code after forcing the lock:

[HUMANOID_VAR].CameraOffset = Vector3.new(1.75, 0, 0)
UserInputService.MouseIconEnabled = false

Replace [HUMANOID_VAR] with the variable representing the player’s humanoid.

Unlocking

To Unlock the player you will need to Call the ForceLock function with false, enable the Mouse icon and reset camera offset. An example is:

playerModule:ForceLock(false)
[HUMANOID_VAR].CameraOffset = Vector3.new(0,0,0)
UserInputService.MouseIconEnabled = true

Summary

By following these steps, you can force shift lock for players using a combination of modifications in the camera and player modules. This method provides a smooth experience and ensures that shift lock is consistently applied.

Note: if you have any improvements please reply with them


9 Likes

Nice tutorial!

Forces the real roblox shiftlock, not toggleable sadly but just incase you’re looking for permanent force shiftlock, i’ve been looking for this a while ago

1 Like

Can’t we just do:

game:GetService("UserInputService").MouseBehaivour = Enum.MouseBhaivour.LockCenter
1 Like

That locks the mouse at the center of the screen, which isn’t the same thing

While trying this method I got an issue with calling the “ToggleShiftLock” from the PlayerModule(self.cameras:ToggleShiftLock). Output said: “attempt to call missing method ‘ToggleShiftLock’ of table”.

I solved it by changing the return of CameraModule from {} to cameraModuleObject.

I don’t know why exactly I got this issue, but I hope my reply will help people that share my problem.

3 Likes

dang man I’ve spent the past 2 hours trying to make my own just to see the top result work :confused:

I’m stuck on step 1… How and where can I find the camera module? I looked in the workspace and see camera only and there is no .new there?

To get the camera module you will want to join the game. Once done it can be found in:
Players(your username)PlayerScriptsPlayermodule

Copy the player module. Stop testing and paste it into:
StarterPlayerStarterPlayerScripts

Within the player module you can find the CameraModule.

1 Like

There is a easier way to do this, without accessing the PlayerModule

You can use UserGameSettings like so:

-- put into a LocalScript inside StarterCharacterScripts
local UserInputService = game:GetService("UserInputService")
local UserGameSettings = UserSettings():GetService("UserGameSettings")

local TOGGLE_KEYCODE = Enum.KeyCode.LeftShift

local humanoid: Humanoid = script.Parent:WaitForChild("Humanoid")
local shoulderCamEnabled = false

local function setShoulderCamEnabled(enabled)
	shoulderCamEnabled = enabled
	
	humanoid.CameraOffset = Vector3.new(enabled and 1.75 or 0, 0, 0)
	UserGameSettings.RotationType = enabled and Enum.RotationType.CameraRelative or Enum.RotationType.MovementRelative
	UserInputService.MouseBehavior = enabled and Enum.MouseBehavior.LockCenter or Enum.MouseBehavior.Default
end

local function toggleShoulderCam()
	setShoulderCamEnabled(not shoulderCamEnabled)
end

UserInputService.InputBegan:Connect(function(object, processed)
	if (processed == false) then
		local keyCode = object.KeyCode
		
		if (keyCode == TOGGLE_KEYCODE) then
			toggleShoulderCam()
		end
	end
end)

This script mimics the default Roblox shift lock behaviour, however you can customise the toggle key, camera offset and more

2 Likes