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
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.