Tutorial: Making an unlock mouse script

Hello Robloxians and devs, if your game is first person and you have GUI which needs to be clicked with a mouse to interact with, then you’re in for a treat. Because in this tutorial, you will learn how to make unlock mouse in first person script. (Please note that this only works in games with locked first person.)

Before scripting

  1. Make a ScreenGui. Optionally, name it MouseUnlock.

  2. Add in a TextButton or ImageButton. Set its active property to false, so that players can’t interact with it.

  3. Set AnchorPoint to {0.5, 0.5} and position to {0.5, 0.5}

  4. Set size to {1,0, 1,0}

  5. (If you used a text button, ignore). Set the image id to 0 and background transparency to 1 so that it’s not visible.

  6. If you used a text button, set background and text transparency to 1.

  7. Select the ScreenGui and set IgnoreGuiInset to true. This will prevent the button being offset to the bottom of the screen.

  8. Move the ScreenGui to StarterGui

Scripting

  1. Insert a LocalScript into StarterGui.

  2. Insert this script:

local mouseUnlock = script.Parent.MouseUnlock.ImageButton --change if necessary
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input)
      if input.KeyCode == Enum.KeyCode.C --you can change the keybind.
            if mouseUnlock.Modal == false then
                mouseUnlock.Modal = true
              else
                 mouseUnlock.Modal = false
          end
     end
end)

How it works:
It will check if C is pressed. If modal is not enabled, it will enable. The Modal property makes it that the player can move their mouse freely in first person mode. If not, it’ll do vice-versa.

Congratulations for completing this tutorial!

11 Likes

If your game has 3rd person, just check if the distance from the camera to the head is less or equal to 1.

2 Likes