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

How to force players into shift lock?

Asked by 4 years ago

Someone asked this question a while back and I found a reddit post talking about it too.

https://www.reddit.com/r/roblox/comments/447k1q/i_found_a_way_to_force_shiftlock/?st=izm321bi&sh=79da45ef

But this has all been changed now that camera script is replaced with CameraModule in PlayerModule. Can someone explain how to do this again because I can't find the same code in the new modules.

1 answer

Log in to vote
2
Answered by
pidgey 548 Moderation Voter
4 years ago
Edited 4 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.

--a script inside StarterCharacterScripts
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)

local player = game.Players.LocalPlayer

local CameraModule = require(player.PlayerScripts.PlayerModule.CameraModule)
local MouseLockController = CameraModule.activeMouseLockController --the LockSwitch library

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)

local player = game.Players.LocalPlayer

local CameraModule = require(player.PlayerScripts.PlayerModule.CameraModule)
local MouseLockController = CameraModule.activeMouseLockController

MouseLockController:DoMouseLockSwitch(0, Enum.UserInputState.Begin) --enable the LockSwitch (2nd argument)

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

local player = game.Players.LocalPlayer

local CameraModule = require(player.PlayerScripts.PlayerModule.CameraModule).new()
local MouseLockController = CameraModule.activeMouseLockController --the LockSwitch library

MouseLockController:DoMouseLockSwitch(0, Enum.UserInputState.Begin) --enable the LockSwitch
MouseLockController:GetBindableToggleEvent():Connect(function() --listen to when the Event is fired (when the MouseLock enabled/disables)
    if not( MouseLockController:IsMouseLocked() ) then --if the mouse is NOT locked
        MouseLockController:DoMouseLockSwitch(0, Enum.UserInputState.Begin) --enable the LockSwitch again!
    end
end)

And that's how I forced the player into Shift Lock. Hope it helps!

0
Also, as a side note, if you want to Disable the LockSwitch, you can call upon DoMouseLockSwitch, but instead of enumeration Enum.UserInputState.Begin, use Enum.UserInputState.Pass, like this: MouseLockController:DoMouseLockSwitch(0, Enum.UserInputState.Pass) pidgey 548 — 4y
0
Holy moly! This was way simpler than I thought it would be but it worked. Thank you so much for a detailed answer and hopefully this helps other people too! tygerupercut3 68 — 4y
0
This does not work anymore because local scripts cant access playerscripts any more right? I get an error saying that playermodule is not child of playerscripts Nogalo 148 — 3y
Ad

Answer this question