Answered by
5 years ago Edited 5 years ago
I just looked through the CameraModule and MouseLockController modules (which can be found in a Player's PlayerScripts > PlayerModule), and found some very useful functions which can make this be done.
I don't know if this is the best way that you can do it, but I found this to be the easiest (i did not like the other solutions).
First, you'll need to require the module which controls the camera (Player > PlayerScripts > PlayerModule > CameraModule), there's a function in there which initializes everything camera-related called new(), so we have to call upon that function to get things started. This will return a metatable.
2 | local CameraModule = require(game.Players.LocalPlayer.PlayerScripts.PlayerModule.CameraModule) |
This script stores the MouseLockController in a variable named activeMouseLockController, so to acquire it, I made a variable named MouseLockController (to avoid confusion)
1 | local player = game.Players.LocalPlayer |
3 | local CameraModule = require(player.PlayerScripts.PlayerModule.CameraModule) |
4 | local MouseLockController = CameraModule.activeMouseLockController |
Now with activeMouseLockController, we can do whatever we want as far as the MouseLockController modulescript goes.
In this case, we want to enable the LockSwitch so it can't be escaped. There's a function named DoMouseLockSwitch in the MouseLockController module which when called upon, locks or unlocks the LockSwitch according to our 2nd argument, to lock it, our 2nd argument upon calling this function needs to be Enum.UserInputState.Begin (the 1st argument can be anything as far as i know)
1 | local player = game.Players.LocalPlayer |
3 | local CameraModule = require(player.PlayerScripts.PlayerModule.CameraModule) |
4 | local MouseLockController = CameraModule.activeMouseLockController |
6 | MouseLockController:DoMouseLockSwitch( 0 , Enum.UserInputState.Begin) |
We can loop this to get our desired result, however, I have a more efficient solution. There's a function named GetBindableToggleEvent in the MouseLockController module, this function returns the RBXScriptSignal for when the Mouse Lock is changed!
So with that in mind we can listen to it, and when it's fired just check if the mouse is locked with another function named IsMouseLocked (this function is self explanatory..)
01 | local player = game.Players.LocalPlayer |
03 | local CameraModule = require(player.PlayerScripts.PlayerModule.CameraModule).new() |
04 | local MouseLockController = CameraModule.activeMouseLockController |
06 | MouseLockController:DoMouseLockSwitch( 0 , Enum.UserInputState.Begin) |
07 | MouseLockController:GetBindableToggleEvent():Connect( function () |
08 | if not ( MouseLockController:IsMouseLocked() ) then |
09 | MouseLockController:DoMouseLockSwitch( 0 , Enum.UserInputState.Begin) |
And that's how I forced the player into Shift Lock. Hope it helps!