Firstperson keeps locking mouse to centre of screen

I have tried many different solutions and none of them has worked I am trying to have a firstperson game with the mouse NOT locked to the centre of the player’s screen. How can I have the mouse to be able to move freely. I have tried the ui frame trick which is having a ui frame that is invisible on the player screen but that didn’t work. My system use to work but now it doesn’t

local script in startgui:

local Plr = game.Players.LocalPlayer
if Plr then
	wait()
	Plr.CameraMode = 0
	Plr.CameraMode = 1
end

local scripts in starterplayerscripts

local player = game.Players.LocalPlayer
local ContextActionService = game:GetService("ContextActionService")

ContextActionService:BindAction(
	"Action",
	function ()
		return Enum.ContextActionResult.Sink
	end,
	false,
	Enum.UserInputType.MouseWheel
)

player.CharacterAdded:Connect(function()
	local plrGui = game.Players.LocalPlayer:WaitForChild("PlayerGui")

	local screenGui = Instance.new("ScreenGui", plrGui)

	local txtButton = Instance.new("TextButton")
	txtButton.BackgroundTransparency = 1
	txtButton.Size = UDim2.new(0, 0, 0, 0)
	txtButton.Text = " "

	txtButton.Modal = true

	txtButton.Parent = screenGui
end)

second one

local UserInputService = game:GetService("UserInputService")

-- Call this when you want to unlock the mouse (e.g. showing UI)
local function unlockMouse()
	UserInputService.MouseBehavior = Enum.MouseBehavior.Default
	UserInputService.MouseIconEnabled = true
end

-- Call this when you want normal gameplay (camera works again)
local function lockMouse()
	UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	UserInputService.MouseIconEnabled = false
end

-- Example: toggle with a key (say, LeftAlt)
UserInputService.InputBegan:Connect(function(input, gpe)
	if gpe then return end
	if input.KeyCode == Enum.KeyCode.LeftAlt then
		if UserInputService.MouseBehavior == Enum.MouseBehavior.LockCenter then
			unlockMouse()
		else
			lockMouse()
		end
	end
end)

There is a lot of solutions but easist is just placing GUI with random Frame having .Model=true and .BackgroundColor3 = 1

Steps

  • Place a ScreenGui in the StarterGui (set ScreenInsets to None) and set IgnoreGuiInset to true
  • Insert a TextButton into it, Set the Size to {1,0,},{1,0} – which means the X.Scale and Y.Scale should be 1, also set the background Transparency to 1, set the Interactable and Active properties to false, also the Visible properly should be true
  • set its Text to “” and set the modal to true
  • now add a local script under the textbutton
  • put this code inside the local script (You can change the binds however you prefer)
local uis=game:GetService"UserInputService"

local keybind=Enum.KeyCode.F

uis.InputBegan:Connect(function(input,gpe)
if gpe then return end

if input.KeyCode==keybind then --if you want to use multiple Keybinds together then use the next comment line instead, remember that's just an example 
--  if uis:IsKeyDown(keybind) and uis:IskeyDown(Enum.KeyCode.LeftControl) then

script.Parent.Parent.Enabled=not script.Parent Parent.Enabled 
end

end)

Note : the mouse is always locked, but when we enable the ScreenGui then the mouse could move freely. so there’s no need to change the mouse behavior as the mouse is always locked in the first person on PC

Create a frame or a button (or whatever has the option Modal), find the property Modal and then set it to true, create a check for player input and set the instance to Visible = true then go from there.

You can set UserInputService.MouseBehavior every frame in RunService.PreRender. This will override whatever is resetting MouseBehavior.

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

local MouseUnlocked = false

UserInputService.InputBegan:Connect(function(Input, GameProcessed)
	if not GameProcessed and Input.KeyCode == Enum.KeyCode.LeftAlt then
		MouseUnlocked = not MouseUnlocked
	end
end)

RunService.PreRender:Connect(function()
	local Behavior =
		if MouseUnlocked then Enum.MouseBehavior.Default
		else Enum.MouseBehavior.LockCenter
	if UserInputService.MouseBehavior ~= Behavior then
		UserInputService.MouseBehavior = Behavior
	end
end)
1 Like

@Yarik_superpro @mbtae1238 @thatguybarny1324
Sadly your solutions did not work as I wasn’t able to click on any of my ui even though I up the Zindex and wasn’t able to move my screen in any way by like right clicking.

Thank you for your help I was able to modify your script so I can enable right clicking to allow my screen to move and once I stop my right click it will allow mouse to move freely again.

The solution i shared is what i use for my own game, i do not know why it didn’t work for you, maybe you did something wrong, overall it was my mistake not being able to give a good explanation.
But honestly i do not prefer to use RunService for such a thing like that as it effects performance and other stuff.

1 Like

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