Shift Lock for Roblox on Mobile Devices

Add support for mouse lock (aka shift lock) for games on Roblox’s mobile app by adding this resource to your game/experience.

I made a handy resource for Roblox developers to support “Mouse Lock” (aka “Shift Lock”) mode in their games for mobile players!

Also consider getting the Model from the Roblox website so it’s available in Studio’s toolbox and on your Roblox account!

View this post on the Roblox Developer Forum instead →

What is mouse lock (shift lock)?

Ozzypig running into the sunset, with shift lock enabled. The cursor is visible just to the right of his head.
Running into the sunset with shift lock on was only possible on desktop – but not anymore!

This handy feature faces your character in the direction your camera is facing, and is highly useful in with melee combat and role-play games. Roblox players who have used the mobile app have probably noticed that the “mouse lock” or “shift lock” doesn’t exist on touch-enabled devices by default. It gives the player far better control over the direction their character is facing than the keyboard or on-screen joystick.

Unfortunately, this great feature is unavailable by default on mobile Roblox. I may not be able to change that, but I can provide fellow Roblox developers with this nice little package which enables them to add it to their games and experiences!

Want your favorite Roblox game to support mobile shift lock? Send them this post! I’ve made this pack super straightforward to install.

What’s in the box?

  1. A replacement PlayerModuleLoader LocalScript for placing in StarterPlayerScripts. This overrides the one Roblox inserts by default with one that does some fiddling before it loads the entire PlayerModule.
  2. An imitation TopBar ScreenGui containing a button that toggles the lock mode. If you don’t want it, you can make your own button using the code in it! It’s just an example that I built to look nice with Roblox’s default UI.
  3. An “INSTRUCTIONS” ModuleScript for those who need the nitty-gritty.

How to enable shift lock

  1. First, insert the object into your place
    • After getting the model from the Roblox website, open the Toolbox and insert it, OR
    • Drag the downloaded model file in your filesystem onto Roblox Studio
  2. Move the PlayerModuleLoader LocalScript into StarterPlayer → StarterPlayerScripts.
  3. Put the MouseLockToggle ScreenGui inside StarterGui.
  4. Stash the original folder with the INSTRUCTIONS in ServerStorage you can find it again, just in case.
  5. That’s it! Test it out by turning on Studio’s device emulator, select a touch device and press play. You should be able to click the button to toggle mouse lock like in the video.

But how?

Lines of code from the CameraModule which limit developer access to the API
You probably never asked for this, but it’s there anyway

This was made possible by EgoMoose’s CameraModule API Public Override. It’s an interesting injection trick which fiddles with a part of the camera portion of the PlayerModule, which is normally locked down by the UserRemoveTheCameraApi FFlag (see above). But, by using some really clever Lua environment injection via the TransparencyController, the CameraModule is essentially tricked into thinking the flag isn’t set. Therefore, it exposes the object to the developer without needing to fork the entire PlayerModule system. Genius!

Lines of code which initialize the MouseLockController only if on a non-TouchEnabled device
CameraModule.new, the part where the MouseLockController isn’t constructed if TouchEnabled is true.

Moreover, I noticed that the constructor for the CameraModule simply didn’t construct a MouseLockController object when running on non-TouchEnabled devices (see above). I added a small bit to the loader script that creates it manually if it got skipped over by the default code. The end result: it works exactly the same now.

Some Fine Print

  • This will probably work for a while, though changes Roblox makes to the camera and mouse-lock parts of the PlayerModule may break or cause issues.
  • I’m not liable if you break your game with this – no warranties included :–)
  • Perhaps in the future Roblox will have formal support for mouse/shift lock for touch – it’s up to you to know whether or not that has happened yet. Search #updates:announcements on the DevForum  just in case.
  • I’m also probably not going to provide support or updates for this. But, you’ve got the wonderfully talented DevForum community is at your fingertips and can help you solve your problems.

Download

Here’s that download button again in case you don’t want to scroll up all the way:

That’s all folks! Hopefully this helps Roblox developers add a well-loved feature to their games.

– Ozzypig

Author: Ozzypig

Roblox developer and computer scientist. I love all things coding and game design!