Mouse stuck in center bug

Does anybody know how to fix this weird bug where the mouse gets stuck in the middle?

It happens when I reset a player after they went into a custom first person script as seen in the video. The only fix I have is the player tabbing out to a different window and tabbing into roblox again but that is highly inefficient…
Edit: I have tried setting MouseBehavior to default instead of LockCenter like in the first person script but that didnt work

You most definitely have a script somewhere constantly setting the MouseBehavior to LockCenter. There’s no other explanation since you aren’t in first person. Maybe use Ctrl+Shift+F (find in all scripts) and look for LockCenter, see if anything pops up

No, it only gets set once when I fire a remote event from the server to start the local script with the first person script

Make the player to unequip the tool first. I think you just delete it from the player backpack

Prevent the script from proceeding once the green background has finished loading

I tried this but it didn’t do anything. I made it so the tool gets unequipped as soon as the other player is dead and vice versa. The tool did get unequipped but the camera stayed the same

No, not really. I can’t stop it unless I reset the player. And if I do reset the player then I get the mouse stuck. I’ll post the first person script here

CanToggleMouse = {allowed = true; activationkey = Enum.KeyCode.F;}
CanViewBody = true
Sensitivity = 0.2
Smoothness = 0.05
FieldOfView = 80	
HeadOffset = CFrame.new(0,0.7,0)
----------------------------------- Settings
task.wait()
local Player = game.Players.LocalPlayer
local Humanoid = Player.Character:FindFirstChild("Humanoid")
local players = game:GetService("Players")
local player = players.LocalPlayer
local rs = game:GetService("ReplicatedStorage")
local FirstPerson1 = rs.FirstPerson1
local rotate = rs.Rotate
--local m = player:GetMouse()
--m.Icon = "http://www.roblox.com/asset/?id=569021388"
local cam = game.Workspace.CurrentCamera
cam.CameraType = Enum.CameraType.Custom
cam.CameraSubject = Humanoid
FirstPerson1.OnClientEvent:Connect(function()
		local Player = game.Players.LocalPlayer
		local Humanoid = Player.Character:FindFirstChild("Humanoid")

		repeat wait() until game:GetService("Players").LocalPlayer.Character ~= nil
		local runService = game:GetService("RunService")
		local input = game:GetService("UserInputService")
		local players = game:GetService("Players")

		local character = player.Character or player.CharacterAdded:wait()
		local human = character.Humanoid
		local humanoidpart = character.HumanoidRootPart

		local head = character:WaitForChild("Head")
		local CamPos,TargetCamPos = cam.CoordinateFrame.p,cam.CoordinateFrame.p 
		local AngleX,TargetAngleX = 0,0
		local AngleY,TargetAngleY = 0,0

		local running = true
		local freemouse = false
		local defFOV = FieldOfView

		local w, a, s, d, lshift = false, false, false, false, false

		local easingtime = 1 --0~1
		local walkspeeds = {
			enabled =		  true;
			walkingspeed =		0;
			backwardsspeed =	0;
			sidewaysspeed =		0;
			diagonalspeed =		0;
			runningspeed =		0;
			runningFOV=			80;}

		local function updatechar()
			for _, v in pairs(character:GetChildren())do
				if CanViewBody then
					if v.Name == 'Head' then
						v.LocalTransparencyModifier = 1
						v.CanCollide = false
						v.face.LocalTransparencyModifier = 1
					end
				else
					if v:IsA'Part' or v:IsA'UnionOperation' or v:IsA'MeshPart' then
						v.LocalTransparencyModifier = 1
						v.CanCollide = false
					end
				end
				if v:IsA'Accessory' then
					v:FindFirstChild('Handle').LocalTransparencyModifier = 1
					v:FindFirstChild('Handle').CanCollide = false
				end
				if v:IsA'Hat' then
					v:FindFirstChild('Handle').LocalTransparencyModifier = 1
					v:FindFirstChild('Handle').CanCollide = false
				end
			end
		end

		local function lerp(a, b, t)
			return a * (1-t) + (b*t)
		end
		input.InputChanged:connect(function(inputObject)
			if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
				local delta = Vector2.new(inputObject.Delta.x/Sensitivity,inputObject.Delta.y/Sensitivity) * Smoothness

				local X = TargetAngleX - delta.y 
				TargetAngleX = (X >= 80 and 80) or (X <= -80 and -80) or X 
				TargetAngleY = (TargetAngleY - delta.x) %360 
			end	
		end)

		input.InputBegan:connect(function(inputObject)
			if inputObject.UserInputType == Enum.UserInputType.Keyboard then
				if inputObject.KeyCode == CanToggleMouse.activationkey then
					if CanToggleMouse.allowed and freemouse == false then
						freemouse = true
					else
						freemouse = false
					end
				end
			end
			if inputObject.UserInputType == Enum.UserInputType.Keyboard then
				if inputObject.KeyCode == Enum.KeyCode.W then
					w = true
				end
				if inputObject.KeyCode == Enum.KeyCode.A then
					a = true
				end
				if inputObject.KeyCode == Enum.KeyCode.S then
					s = true
				end
				if inputObject.KeyCode == Enum.KeyCode.D then
					d = true
				end
			end
		end)

		input.InputEnded:connect(function(inputObject)
			if inputObject.UserInputType == Enum.UserInputType.Keyboard then
				if inputObject.KeyCode == Enum.KeyCode.W then
					w = false
				end
				if inputObject.KeyCode == Enum.KeyCode.A then
					a = false
				end
				if inputObject.KeyCode == Enum.KeyCode.S then
					s = false
				end
				if inputObject.KeyCode == Enum.KeyCode.D then
					d = false
				end
			end
		end)

		runService.RenderStepped:connect(function()
			if running then
				updatechar()
				CamPos = CamPos + (TargetCamPos - CamPos) *0.28 
				AngleX = AngleX + (TargetAngleX - AngleX) *0.35 
				local dist = TargetAngleY - AngleY 
				dist = math.abs(dist) > 180 and dist - (dist / math.abs(dist)) * 360 or dist 
				AngleY = (AngleY + dist *0.35) %360
				cam.CameraType = Enum.CameraType.Scriptable
				cam.CoordinateFrame = CFrame.new(head.Position) 
					* CFrame.Angles(0,math.rad(AngleY),0) 
					* CFrame.Angles(math.rad(AngleX),0,0)
					* HeadOffset
				humanoidpart.CFrame=CFrame.new(humanoidpart.Position)*CFrame.Angles(0,math.rad(AngleY),0)
			else game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.Default
			end
			if (cam.Focus.p-cam.CoordinateFrame.p).magnitude < 1 then
				running = false
			else
				running = true
				if freemouse == true then
					game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.Default
				else
					game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.LockCenter
				end
			end
			if not CanToggleMouse.allowed then
				freemouse = false
			end
			cam.FieldOfView = FieldOfView
			if walkspeeds.enabled then
				if w and s then return end
				if w and not lshift then
					FieldOfView = lerp(FieldOfView, defFOV,easingtime)
					human.WalkSpeed = 0
				elseif w and a then
					human.WalkSpeed = 0
				elseif w and d then
					human.WalkSpeed = 0
				elseif s then
					human.WalkSpeed = 0
				elseif s and a then
					human.WalkSpeed = 0
				elseif s and d then
					human.WalkSpeed = 0
				elseif d then
					human.WalkSpeed = 0
				elseif a then
					human.WalkSpeed = 0
				end	
				if lshift and w then
					FieldOfView = lerp(FieldOfView, walkspeeds.runningFOV,easingtime)
					human.WalkSpeed = 0
			end
		end
	end)
end)

i did kinda forgot to complete the code

Instead of the whole code I put this in?
Edit: Putting only that in makes the camera not even go into first person

A couple of things you could try:

  1. Set the player’s CameraMode. playerObject.CameraMode = Enum.CameraMode.Default
  2. Control a TextButton, size {0, 0}, {0, 0}, and toggle the Modal property through the script.

You could try including a script that runs when a player dies or resets which sets their mouseBehavior to default. Ideally you would want this built into the first person script itself but it can also work as a separate script (make sure it’s a localscript in StarterCharacterScripts). Something like:

local humanoid = script.Parent:WaitForChild("Humanoid")

function fixCamera()
	game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.Default
end

humanoid.Died:Connect(fixCamera)

It’ll maintain the particular behavior of the script you’re using when the camera is working as intended, I think.

1 Like

Yep, this works. I put that same code but in Player gui and it didn’t work at all so I guess it matters a lot in which part of the game it is.

Starter character scripts are inside the player’s character model when they load, while starter GUI goes to the player’s GUI folder. The character has the humanoid, which holds information about the player’s current state eg. health and speed, so things like code that runs on death makes more sense to include in the starter character where it’s quickly accessed.

In this case, the code only works in starter character scripts because it finds its parent (the player model) and looks for a humanoid inside of it. There is no humanoid inside the gui, and even if there was it would not be helpful since it would not be the humanoid that actually defines the player’s status, which is why it didn’t work.

Since you seem to be doing things with the player’s character, I suggest learning more about what the different starter folders do. It’ll be very helpful later.

1 Like

Just gonna say this if anyone has the same problem and this isn’t working for him. This also didn’t work for me, but by making an error in the local script the mouse gets reset to default for some reason. The way I got the error to be made is instead of using

Humanoid.Died:Connect(fixCamera)

I pass a nil value to the function to create an error

Humanoid.Died:Connect(fixCamera())

And this fixed the problem with the mouse getting stuck in the middle

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