Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do you know when shift lock is turned on or off?

Asked by 5 years ago
Edited 5 years ago

I haven't seen any questions asking this anywhere, so I might be the first to ask it

2 answers

Log in to vote
2
Answered by
y3_th 176
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.

1--MouseLockController in CameraModule
2function MouseLockController:GetIsMouseLocked()
3    return self.isMouseLocked
4end

In order to access this function, you have to get the CameraModule through the PlayerModule by calling PlayerModule:GetCameras().

1--LocalScript in StarterPlayerScripts
2local PlayerModule = require(script.Parent:WaitForChild("PlayerModule")).new()
3local 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.

01--LocalScript in StarterPlayerScripts
02local USER_INPUT_SERVICE = game:GetService("UserInputService")
03 
04local PlayerModule = require(script.Parent:WaitForChild("PlayerModule")).new()
05local CameraModule = PlayerModule:GetCameras()
06local TouchEnabled   = USER_INPUT_SERVICE.TouchEnabled
07local MouseLockController
08 
09if not TouchEnabled then
10    MouseLockController = CameraModule.activeMouseLockController
11end

If you just want to check if shift lock is enabled at certain times, just call MouseLockController:GetIsMouseLocked() after checking that the MouseLockController exists.

1local MouseLockEnabled
2if MouseLockController then
3    MouseLockEnabled = MouseLockController:GetIsMouseLocked()
4end

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.

01--LocalScript in StarterPlayerScripts
02local USER_INPUT_SERVICE = game:GetService("UserInputService")
03 
04local PlayerModule = require(script.Parent:WaitForChild("PlayerModule")).new()
05local CameraModule = PlayerModule:GetCameras()
06local TouchEnabled = USER_INPUT_SERVICE.TouchEnabled
07local MouseLockController, MouseLockEvent, MouseLockEnabled
08 
09function onToggleMouseLock()
10    MouseLockEnabled = MouseLockController:GetIsMouseLocked()
11    print(MouseLockEnabled)
12end
13 
14if not TouchEnabled then
15    MouseLockController = CameraModule.activeMouseLockController
16    MouseLockEvent = MouseLockController:GetBindableToggleEvent()
17    MouseLockEvent:Connect(onToggleMouseLock)
18end

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

Ad
Log in to vote
1
Answered by
starmaq 1290 Moderation Voter
5 years ago

In fact this has been answered many times, like this post. You simply have to check if the MouseBehaviour is equal to Enum.MouseBehavior.LockCenter. In other words if the mouse is locked and center, meaning shift lock is enabled, do something.

1local UserInputService = game:GetService("UserInputService")
2 
3    while true do
4       wait()
5        if UserInputService.MouseBehavior == Enum.MouseBehavior.LockCenter then
6           --do stuff
7       end
8end
0
Doesn't it also do this for first-person? y3_th 176 — 5y
0
good point actually, should've thought about that starmaq 1290 — 5y

Answer this question