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)
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.
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)
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")
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 Please give me some feedback, thanks!
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.
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
while wait() do
if game.StarterPlayer.CameraMode == "LockFirstPerson" then
print("player is in first person")
else
print("player is not in first person")
end