How do I detect if a player is in first person?

I want to run this code when it is detected the client has went into first person.
How would I go about doing this?

local player = game.Players.LocalPlayer

game:GetService("RunService").RenderStepped:connect(function()
	local c = player.Character
	if c then
	c["Left Leg"].LocalTransparencyModifier = 0
	c["Left Arm"].LocalTransparencyModifier = 0
	c["Right Leg"].LocalTransparencyModifier = 0 
	c["Right Arm"].LocalTransparencyModifier = 0
	c["Torso"].LocalTransparencyModifier = 0
	local human = c:WaitForChild("Humanoid")
	human.CameraOffset = Vector3.new(0,0,-1)
	end	
end)
6 Likes

I think it might work if you used logic checking the magnitude between the head’s position and the camera’s position:

local camera = workspace.CurrentCamera
local head = game.Players.LocalPlayer.Character:WaitForChild("Head")
local function IsFirstPerson()
     return (head.CFrame.p - camera.CFrame.p).Magnitude < 1
end

I tested this logic and it seems to work good. If the length of the vector is smaller than 1, it’s safe to assume the player is in first person. A better solution may be available, but from what I’ve tested, this seems to work reliably.

27 Likes

do I put my code after return?

1 Like

I wrote a simple function that you can call to determine if a player is in first person or not. To apply that with your current code, it might look something like…

game:GetService("RunService").RenderStepped:connect(function()
	local c = player.Character
	if c and IsFirstPerson() then
	c["Left Leg"].LocalTransparencyModifier = 0
	c["Left Arm"].LocalTransparencyModifier = 0
	c["Right Leg"].LocalTransparencyModifier = 0 
	c["Right Arm"].LocalTransparencyModifier = 0
	c["Torso"].LocalTransparencyModifier = 0
	local human = c:WaitForChild("Humanoid")
	human.CameraOffset = Vector3.new(0,0,-1)
	end	
end)
1 Like

I’m getting an error on this line:
local head = game.Players.LocalPlayer.Character:WaitForChild(“Head”)
[20:34:19.962 - Players.Zvtu.Backpack.FirstPerson:2: attempt to index nil with ‘Head’]
My code: (In a localscript in starterpack)

local camera = workspace.CurrentCamera

local head = game.Players.LocalPlayer.Character:WaitForChild("Head")

local function IsFirstPerson()

game:GetService("RunService").RenderStepped:connect(function()

local c = game.Players.LocalPlayer.Character

if c and IsFirstPerson() then

c["Left Leg"].LocalTransparencyModifier = 0

c["Left Arm"].LocalTransparencyModifier = 0

c["Right Leg"].LocalTransparencyModifier = 0

c["Right Arm"].LocalTransparencyModifier = 0

c["Torso"].LocalTransparencyModifier = 0

local human = c:WaitForChild("Humanoid")

human.CameraOffset = Vector3.new(0,0,-1)

return (head.CFrame.p - camera.CFrame.p).Magnitude < 1
end
end)
end

Don’t wrap your RenderStepped event inside of the function I wrote. Use the example I gave you.

You could also replace the ‘head’ line with this:

local head = (game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()) and game.Players.LocalPlayer.Character:WaitForChild("Head")
8 Likes

Now that I think about this problem, I found a solution that maybe you can try. All you need to do is just check the LocalTransparencyModifier of the head because Roblox changes it to 1 when you are in first person view:

if head.LocalTransparencyModifier == 1 then
    -- In first person view
else
    -- Not in first person view
end

This is my first post :smiley: Please give me some feedback, thanks!

59 Likes

Did you try your solution beforehand? I’ve tried your solution but sadly it doesn’t work. It will only work if you locked the player to first person view. And if you locked it to fpv, you don’t need this script to begin with. Player.CameraMode just states the camera mode the player is in, not whether the player is currently in fpv.

You can try this:

local function isFirstPerson()
local camera = workspace.CurrentCamera
local character = game:GetService(“Players”).LocalPlayer.Character or game:GetService(“Players”).LocalPlayer.CharacterAdded
if (character.Head.CFrame.p - camera.CFrame.p).magnitude < 1 then
return true
else
return false
end
end

while wait() do
local isFirstPerson = isFirstPerson()

if isFirstPerson == true then
    print("Player is now in first person.")
end

end

2 Likes

try this

while wait() do 
     if game.StarterPlayer.CameraMode == "LockFirstPerson" then
  print("player is in first person")
     else
   print("player is not in first person")
end

This won’t work, as it is checking if it is LOCKED in first person, not if the player is currently in first person.

7 Likes