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 4 years ago
Edited 4 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
4 years ago
Edited 4 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.

--MouseLockController in CameraModule
function MouseLockController:GetIsMouseLocked()
    return self.isMouseLocked
end

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

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

--LocalScript in StarterPlayerScripts
local USER_INPUT_SERVICE = game:GetService("UserInputService")

local PlayerModule = require(script.Parent:WaitForChild("PlayerModule")).new()
local CameraModule = PlayerModule:GetCameras()
local TouchEnabled   = USER_INPUT_SERVICE.TouchEnabled
local MouseLockController

if not TouchEnabled then
    MouseLockController = CameraModule.activeMouseLockController
end

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

local MouseLockEnabled
if MouseLockController then
    MouseLockEnabled = MouseLockController:GetIsMouseLocked()
end

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.

--LocalScript in StarterPlayerScripts
local USER_INPUT_SERVICE = game:GetService("UserInputService")

local PlayerModule = require(script.Parent:WaitForChild("PlayerModule")).new()
local CameraModule = PlayerModule:GetCameras()
local TouchEnabled = USER_INPUT_SERVICE.TouchEnabled
local MouseLockController, MouseLockEvent, MouseLockEnabled

function onToggleMouseLock()
    MouseLockEnabled = MouseLockController:GetIsMouseLocked()
    print(MouseLockEnabled)
end

if not TouchEnabled then
    MouseLockController = CameraModule.activeMouseLockController
    MouseLockEvent = MouseLockController:GetBindableToggleEvent()
    MouseLockEvent:Connect(onToggleMouseLock)
end

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

local UserInputService = game:GetService("UserInputService")

    while true do
       wait()
        if UserInputService.MouseBehavior == Enum.MouseBehavior.LockCenter then
           --do stuff
       end
end
0
Doesn't it also do this for first-person? y3_th 176 — 4y
0
good point actually, should've thought about that starmaq 1290 — 4y

Answer this question