Answered by
5 years ago Edited 5 years ago
The new PlayerModule update makes this very easy. If you take a look in the MouseLockController script under the CameraModule, you will find a function that allows you to get if the mouse is locked or not.
2 | function MouseLockController:GetIsMouseLocked() |
3 | return self.isMouseLocked |
In order to access this function, you have to get the CameraModule through the PlayerModule by calling PlayerModule:GetCameras()
.
2 | local PlayerModule = require(script.Parent:WaitForChild( "PlayerModule" )).new() |
3 | local CameraModule = PlayerModule:GetCameras() |
In order to access the MouseLockController, you have to first check if the player is not on a mobile device (so we don't accidentally index nil) and then get the controller, which is CameraModule.activeMouseLockController.
02 | local USER_INPUT_SERVICE = game:GetService( "UserInputService" ) |
04 | local PlayerModule = require(script.Parent:WaitForChild( "PlayerModule" )).new() |
05 | local CameraModule = PlayerModule:GetCameras() |
06 | local TouchEnabled = USER_INPUT_SERVICE.TouchEnabled |
07 | local MouseLockController |
09 | if not TouchEnabled then |
10 | MouseLockController = CameraModule.activeMouseLockController |
If you just want to check if shift lock is enabled at certain times, just call MouseLockController:GetIsMouseLocked()
after checking that the MouseLockController exists.
2 | if MouseLockController then |
3 | MouseLockEnabled = MouseLockController:GetIsMouseLocked() |
However, if you want to update the value every time shift lock is toggled, there's a BindableEvent you can get from the MouseLockController by calling MouseLockController:GetBindableToggleEvent()
, and you can connect functions to it.
02 | local USER_INPUT_SERVICE = game:GetService( "UserInputService" ) |
04 | local PlayerModule = require(script.Parent:WaitForChild( "PlayerModule" )).new() |
05 | local CameraModule = PlayerModule:GetCameras() |
06 | local TouchEnabled = USER_INPUT_SERVICE.TouchEnabled |
07 | local MouseLockController, MouseLockEvent, MouseLockEnabled |
09 | function onToggleMouseLock() |
10 | MouseLockEnabled = MouseLockController:GetIsMouseLocked() |
11 | print (MouseLockEnabled) |
14 | if not TouchEnabled then |
15 | MouseLockController = CameraModule.activeMouseLockController |
16 | MouseLockEvent = MouseLockController:GetBindableToggleEvent() |
17 | MouseLockEvent:Connect(onToggleMouseLock) |
If you test the game, you should find that the script outputs "true" when shift lock is on and outputs "false" when shift lock is off. This is the desired output.
EDIT: fixed some errors in formatting