(Solved)How to make a R to unlock and lock mouse pointer in first person

  1. What do you want to achieve? R to unlock and lock mouse pointer in first person

  2. What is the issue? Cannot create this on my own since i only know a few scripts game.Workspace.blahblahblah.visible = false ect
    (im new idk how to do much)

2 Likes

Check out the UserInputService api documentation
There’s a property called MouseBehaviour which you can set to LockCenter/Default with

--Locking mouse to center
UserInputService.MouseBehaviour = Enum.MouseBehaviour.LockCenter

Unlocking mouse
UserInputService.MouseBehaviour = Enum.MouseBehaviour.Default

And all you gotta do is run this code in an InputBegan event

1 Like

Wait whats a inputBegan event?

I highly suggest you to first learn events/ functions/ variable usage first or nothing i say is going to make any sense to you.

1 Like

yeah idk where to use the code or in what function or event or whatever

Primarily it would be used in this type of way for locking/unlocking the mouse

local UIS = game:GetService("UserInputService")

local MouseLocked = false

UIS.InputBegan:Connect(function(input, gameProcessed)
   if gameProcessed then return end --exit function if they are typing

   if input.KeyCode == Enum.KeyCode.R then
      MouseLocked = not MouseLocked --turn mouselocked to true if false, false if true

      if MouseLocked then --if true then lock mouse
         UIS.MouseBehaviour = Enum.MouseBehaviour.LockCenter
      else
         UIS.MouseBehaviour = Enum.MouseBehaviour.Default
      end
   end
end)
1 Like

Would it work in first person?

It does not matter if it’s in first/third person, if the code is in a localscript then the mouse will be locked/unlocked when pressing r

the script does not work for me

image

The overall problem is more difficult as you will need to modify the BaseCamera module script as well to remove the default behavior of lock center in first person which is forced every frame by the module.

This is pretty advanced so I’ll give the script in it to show how you can edit the default player scripts.

However @TenBlocke provides a good starting point which I have used it had a small error of changing MouseBehaviour → MouseBehavior just spelling differences between US and UK.

BaseCamera location
image

Result:

Local Script belongs in starter player scripts:

--Copy and paste into starter player scripts
local UIS = game:GetService("UserInputService")

--Camera module services
local cameraModule = script.Parent:WaitForChild("PlayerModule"):WaitForChild("CameraModule")

local CameraUtils = require(cameraModule:WaitForChild("CameraUtils"))
local BaseCamera = require(cameraModule:WaitForChild("BaseCamera"))
local CameraUI = require(cameraModule:WaitForChild("CameraUI"))
local CameraInput = require(cameraModule:WaitForChild("CameraInput"))
local CameraToggleStateController = require(cameraModule:WaitForChild("CameraToggleStateController"))

local UserGameSettings = UserSettings():GetService("UserGameSettings")

local MouseLocked = false

local mouseBehavior = Enum.MouseBehavior.LockCenter
UIS.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end --exit function if they are typing

	if input.KeyCode == Enum.KeyCode.R then
		MouseLocked = not MouseLocked --turn mouselocked to true if false, false if true

		if MouseLocked then --if true then lock mouse
			mouseBehavior = Enum.MouseBehavior.LockCenter
			UIS.MouseBehavior = Enum.MouseBehavior.LockCenter

		else
			mouseBehavior = Enum.MouseBehavior.Default
			UIS.MouseBehavior = Enum.MouseBehavior.Default
		end
	end
end)

local debounce = false

function BaseCamera:UpdateMouseBehavior()
	local blockToggleDueToClickToMove = UserGameSettings.ComputerMovementMode == Enum.ComputerMovementMode.ClickToMove

	if self.isCameraToggle and blockToggleDueToClickToMove == false then
		CameraUI.setCameraModeToastEnabled(true)
		CameraInput.enableCameraToggleInput()
		CameraToggleStateController(self.inFirstPerson)
	else
		CameraUI.setCameraModeToastEnabled(false)
		CameraInput.disableCameraToggleInput()

		-- first time transition to first person mode or mouse-locked third person
		if self.inFirstPerson or self.inMouseLockedMode then
			CameraUtils.setRotationTypeOverride(Enum.RotationType.CameraRelative)
			if not debounce then
				debounce = true
				CameraUtils.setMouseBehaviorOverride(mouseBehavior)
			end
		else
			debounce = false
			CameraUtils.restoreRotationType()
			CameraUtils.restoreMouseBehavior()
		end
	end
end
19 Likes

Thank you i needed this so i could have pressable buttons in my horror game

4 Likes

Apologies for bringing back the topic, but if you don’t want the mouse to go back to the middle when you stop moving your mouse, you can simply remove one line of code, which is the CameraUtils.restoreMouseBehavior() function at the bottom. This is the code:

--Copy and paste into starter player scripts
local UIS = game:GetService("UserInputService")

--Camera module services
local cameraModule = script.Parent:WaitForChild("PlayerModule"):WaitForChild("CameraModule")

local CameraUtils = require(cameraModule:WaitForChild("CameraUtils"))
local BaseCamera = require(cameraModule:WaitForChild("BaseCamera"))
local CameraUI = require(cameraModule:WaitForChild("CameraUI"))
local CameraInput = require(cameraModule:WaitForChild("CameraInput"))
local CameraToggleStateController = require(cameraModule:WaitForChild("CameraToggleStateController"))

local UserGameSettings = UserSettings():GetService("UserGameSettings")

local MouseLocked = false

local mouseBehavior = Enum.MouseBehavior.LockCenter
UIS.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end --exit function if they are typing

	if input.KeyCode == Enum.KeyCode.R then
		MouseLocked = not MouseLocked --turn mouselocked to true if false, false if true

		if MouseLocked then --if true then lock mouse
			mouseBehavior = Enum.MouseBehavior.LockCenter
			UIS.MouseBehavior = Enum.MouseBehavior.LockCenter

		else
			mouseBehavior = Enum.MouseBehavior.Default
			UIS.MouseBehavior = Enum.MouseBehavior.Default
		end
	end
end)

local debounce = false

function BaseCamera:UpdateMouseBehavior()
	local blockToggleDueToClickToMove = UserGameSettings.ComputerMovementMode == Enum.ComputerMovementMode.ClickToMove

	if self.isCameraToggle and blockToggleDueToClickToMove == false then
		CameraUI.setCameraModeToastEnabled(true)
		CameraInput.enableCameraToggleInput()
		CameraToggleStateController(self.inFirstPerson)
	else
		CameraUI.setCameraModeToastEnabled(false)
		CameraInput.disableCameraToggleInput()

		-- first time transition to first person mode or mouse-locked third person
		if self.inFirstPerson or self.inMouseLockedMode then
			CameraUtils.setRotationTypeOverride(Enum.RotationType.CameraRelative)
			if not debounce then
				debounce = true
				CameraUtils.setMouseBehaviorOverride(mouseBehavior)
			end
		else
			debounce = false
			CameraUtils.restoreRotationType()
			CameraUtils.restoreMouseBehavior()
		end
	end
end

Credits to @dthecoolest for the original code. I just modified it.

3 Likes