Forcing Shiftlock

For awhile now i’ve seen threads with questions about forcing shiftlock in games and how to do it, after spending some time working on my combat game I’m pretty positive i’ve found a decent solution.

Here’s what I did:
For starters I wanted the shiftlock to toggle whenever I set a certain boolean to true, so I created a folder in StarterPlayerScripts with the boolean inside
image

After this I needed to make it actually do something, so I created a local script inside of StarterCharacterScripts and set it up so that on every frame it checks to see if the boolean is set to true or not, and sets a customized offset. I decided to do it like this because I needed to be able to adjust it later on and give myself more customization.

Here is an edited version of that script with the basic premise of it (I have since far edited it with more parts to it, I’m only here to show how to make the shift lock)

--[[SERVICES]]--
local runService = game:GetService("RunService")
local players = game:GetService("Players")

--[[VARIABLES]]--
--Player Variables--
local player = players.LocalPlayer
local humanoid = player.Character.Humanoid

--Camera Variables--
local originalCameraOffset = Vector3.new(0,0,0) --The original camera offset we want
local cameraOffset = Vector3.new(2,0,0) --Offset we want the camera to be set at when shift locked

--Shiftlock Variables--
local valueFolder = player.PlayerScripts.Values
local shiftLockOn = valueFolder.ShiftLock

local function shiftLockSetup()
	if shiftLockOn.Value == true then
		humanoid.CameraOffset = cameraOffset
	else shiftLockOn.Value == false  then
		humanoid.CameraOffset = originalCameraOffset
	end
end

runService.RenderStepped:Connect(shiftLockSetup)

Awesome, we have our offset working. The problem we have now is locking the mouse to the camera.
There are other ways to do this, but I chose to directly modify the Camera Module to lock the mouse to the character whenever the boolean is turned on, specifically line 469 in the CameraModule:Update() function. Here is the code snippet I used to replace the previous one.

function CameraModule:Update(dt)
	if self.activeCameraController then
		if shiftLockBool.Value == true then --Edited code, simple if-else statement
			self.activeCameraController:SetIsMouseLocked(true) --locks mouse to player
		else
			self.activeCameraController:SetIsMouseLocked(false)
		end
		if FFlagUserCameraToggle then
			self.activeCameraController:UpdateMouseBehavior()
		end

		local newCameraCFrame, newCameraFocus = self.activeCameraController:Update(dt)
		self.activeCameraController:ApplyVRTransform()
		if self.activeOcclusionModule then
			newCameraCFrame, newCameraFocus = self.activeOcclusionModule:Update(dt, newCameraCFrame, newCameraFocus)
		end

		-- Here is where the new CFrame and Focus are set for this render frame
		game.Workspace.CurrentCamera.CFrame = newCameraCFrame
		game.Workspace.CurrentCamera.Focus = newCameraFocus

		-- Update to character local transparency as needed based on camera-to-subject distance
		if self.activeTransparencyController then
			self.activeTransparencyController:Update()
		end
	end
end

I defined the shiftLockBool at the beginning of the module script.

Now, what this is doing is every time the camera is updated now if that boolean is set to true it will automatically lock the mouse, with the best part being the character will now move with the mouse.

When the these two scripts are put together now, the boolean will toggle shiftlock.

Best part about this is you don’t need to rescript an entire complicated thing for rotating the character around with the camera or write any complicated scripts, Roblox has already done half of it for you. On top of this this will always work, even if the toggle shiftlock option is turned off in game, allowing you complete control over shiftlocking the camera, and even on top of that this works on all platforms.

This is a sort of hacky way of doing it I suppose, but this can be easily expanded upon for multiple uses.

22 Likes

Can you provide a video of the result and why UserInputService.MouseBehaviour can’t be utilised at all?

8 Likes

Yes, and I tried UserInputService.MouseBehaviour but it didn’t move the character with the camera. I would’ve had to create a script where it moved the player with the camera.

My PC doesn’t have the spec to screen record, but I can give you a Gif

It won’t let me upload a gif or video, but I can put a link to the testing place I have that uses this

I figured that may be the case, it’s just hard to understand fully what you mean and why your character didn’t move with the camera when using UIS without seeing any visuals. Graphics, videos and images help a lot when it comes to tutorials.

Definitely include a link to a demo if you can.

1 Like

This is the game that I created implementing this.

1 Like

Does UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter not accomplish the same thing?

local UserInputService = game:GetService("UserInputService")
local BasicState = require(path.to.BasicState)

local State = BasicState.new({
    MouseLocked = false
})

State:GetChangedSignal("MouseLocked"):Connect(function(MouseLocked)
    UserInputService.MouseBehavior = MouseLocked and Enum.MouseBehavior.LockCenter or Enum.MouseBehavior.Default
end)

State:Set("MouseLocked", true)

wait(5)
State:Set("MouseLocked", false)

--> Locks mouse for 5 seconds then releases.
3 Likes

It doesn’t move the player’s character with it.

3 Likes

How would I edit the script so that you only go into shiftlock when you have a weapon equipped???

Use the .Equipped event on the tool and set the ShiftLock value’s value to true. Do the opposite with the .Unequipped event to disable it.

2 Likes

where does the other script go?