Unlocking the player's mouse in first person

Good evening, I’m trying to make a script that’ll toggle a player’s mouse behavior (at least I believe that’s what it should do to unlock the mouse), that should work like this:

--LocalScript in StarterPlayerScripts
local UIS = game:GetService("UserInputService")
local cam = workspace.Camera

local mouseunlocked = Instance.new("BoolValue", game.Players.LocalPlayer)
mouseunlocked.Name = "MouseUnlocked"
mouseunlocked.Value = false
--Using a bool value for the "Changed" event, not sure what else I can try using here to tell whether it's changed to true or false

UIS.InputBegan:Connect(function(input, gPE)
	if input.KeyCode == Enum.KeyCode.F then
		mouseunlocked.Value = not mouseunlocked.Value
	end
end)

mouseunlocked.Changed:Connect(function()
	if mouseunlocked.Value == true then
		-- Mouse is unlocked for both first and third person, meaning it should be able to move freely across the screen
	else
		UIS.MouseBehavior = Enum.MouseBehavior.Default -- I believe setting it to default will turn it back to normal
	end
end)

Main issue is that I don’t know how to unlock the mouse in first person when the mouseunlocked value is set to true, would appreciate any help :grinning_face_with_smiling_eyes:

1 Like

forgot to add a line for the gameProcessedEvent (gPE), however I’m also not sure what it does, but I think it’s used to determine whether the player is typing or not so you dont accidentally activate the keybind while chatting (somebody please tell me what it does)

Try this instead!

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local player = Players.LocalPlayer

local mouseUnlocked = Instance.new("BoolValue", player)
mouseUnlocked.Name = "MouseUnlocked"
mouseUnlocked.Value = false

UserInputService.InputBegan:Connect(function(input, processed)
	if processed then return end

	if input.KeyCode == Enum.KeyCode.F then
		mouseUnlocked.Value = not mouseUnlocked.Value
	end
end)

RunService:BindToRenderStep("MouseBehaviorCustom", Enum.RenderPriority.Camera.Value + 1, function()
	if mouseUnlocked.Value then
		UserInputService.MouseBehavior = Enum.MouseBehavior.Default
	end
end)

Roblox’s PlayerModule will set the mouse behavior every frame because of the camera. If you’re in first person, it uses Enum.MouseBehavior.LockCenter, otherwise it uses the default. You can change override this by setting the MouseBehavior after the camera updates! In the code, I’ve set the MouseBehavior to be Default when the mouseUnlocked.Value is true. No else is required here, because if you don’t change the mouse behavior, it will use whatever the PlayerModule set it to.

5 Likes

that works, but another question comes up; is there any way to let the player also move their camera by holding rmb like you do in third person? (while mouse unlocked)

Yeah, use Enum.MouseBehavior.LockCurrentPosition for this! This will lock the mouse (allowing the camera to rotate), but it will lock in place instead of moving to the center of the screen.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.