How to detect a Player in First Person?

Is there a way to detect if a player is in first person, can someone show me an example?

1 Like

Do you want to know on the server or the client.

2 Likes

you can use this method in a loop its pretty simple:

	if (cam.CFrame.p - cam.Focus.p).Magnitude < 0.6 then
		print("fps")
	else
		print("not fps")
	end
6 Likes

I doubt there is a better way than using while loop, checking if camera’s distance from head is lower than 1 (or 0.8, 0.6, …). It’s not a perfect way, however.

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

local Client = Players.LocalPlayer
local Camera = workspace.CurrentCamera

local Character = Client.Character or Client.CharacterAdded:Wait() 
local Head = Character:WaitForChild("Head") 

local function OnRenderStepped()
	if (Camera.CFrame.Position - Head.Position).Magnitude < 0.8 then
		print("In First Person") 
	end
end

RunService.RenderStepped:Connect(OnRenderStepped) 
3 Likes

A better way would be to only check when the user makes some kind of input to change their zoom (scroll wheel, pinching on touch, don’t even know what on gamepad). The method of checking distance to head is also flawed, because it can be < 0.6 without being in first-person mode due to wall clipping / the camera being pushed closer because of walls being in the way.

3 Likes

Alright, so a while ago Roblox decided to disable the CameraModule API so developers can’t access it to e.g. modify how it works or access methods to properly detect things like shiftlock or first person. A kind person made a fix for this, which you can find here: GitHub - EgoMoose/patch-roblox-cameramodule: A module to patch access to the Roblox PlayerModule camera API and here: CameraModule API Public Override

Once you add that fix to your game, you can check if the local player is in first person using the code that someone posted in this comment: Do *NOT* remove the CameraModule API? - #4 by homermafia1

All of this lets you do is_first_person() to properly check if the local player is in first person mode.

5 Likes
if player.CameraMode == Enum.CameraMode.LockInFirstPerson then
    -do something here
end

Obviously you need a player variable.

game.UserInputService.MouseBehavior == Enum.MouseBehavior.LockCenter

1 Like