First person self-view script not working

Hello, developers. Today I was making a script that allows you too see yourself in first person. I made a code and I was wondering, why doesn’t that code work. Then I changed the whole entire code and started from scrath and made a new one. And that one worked. Here is the code that doesn’t work:
– initialize local variables
local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character.Humanoid

-- camera settings
player.CameraMaxZoomDistance = 0.5 -- force first person
camera.FieldOfView = 100
humanoid.CameraOffset = Vector3.new(0, 0, -1)
 
-- set and keep every body part Transparency to its real transparency
for childIndex, child in pairs(character:GetChildren()) do
    if child:IsA("BasePart") and child.Name ~= "Head" then
       
        child:GetPropertyChangedSignal("LocalTransparencyModifier"):Connect(function()
            child.LocalTransparencyModifier = child.Transparency
        end)
       
        child.LocalTransparencyModifier = child.Transparency
       
    end
end
 
-- if the player steps in a vehicle
camera:GetPropertyChangedSignal("CameraSubject"):Connect(function()
    if camera.CameraSubject:IsA("VehicleSeat") then
        camera.CameraSubject = humanoid
    end
end)

And here is the code that works:
local Player = game.Players.LocalPlayer

game:GetService("RunService").RenderStepped:connect(function()
	local c = game.Workspace:FindFirstChild(Player.Name)
	if c then
		c["LeftFoot"].LocalTransparencyModifier = 0
		c["LeftHand"].LocalTransparencyModifier = 0
		c["LeftLowerArm"].LocalTransparencyModifier = 0
		c["LeftLowerLeg"].LocalTransparencyModifier = 0
		c["LeftUpperArm"].LocalTransparencyModifier = 0
		c["LeftUpperLeg"].LocalTransparencyModifier =0
		c["RightFoot"].LocalTransparencyModifier = 0 
		c["RightHand"].LocalTransparencyModifier = 0
		c["RightLowerArm"].LocalTransparencyModifier = 0
		c["RightLowerLeg"].LocalTransparencyModifier = 0
		c["RightUpperArm"].LocalTransparencyModifier = 0
		c["RightUpperLeg"].LocalTransparencyModifier = 0
	end
end)

I just wanted to know why one of them doesn’t work and the other does work.

Placing the first code in a LocalScript in StarterCharacterScripts seemed to work for me, so I’m guessing you placed it in another Service.

I’ve edited the code to work for it being placed in let’s say StarterPlayerScripts, and it’s worked too:

-- initialize local variables
local camera = workspace.CurrentCamera
local player = game:GetService("Players").LocalPlayer

local function CharacterAdded(character)
	--// wait for the humanoid to ensure it exists
	local humanoid = character:WaitForChild("Humanoid")

	-- camera settings
	player.CameraMaxZoomDistance = 0.5 -- force first person
	camera.FieldOfView = 100
	humanoid.CameraOffset = Vector3.new(0, 0, -1)

	-- set and keep every body part Transparency to its real transparency
	for childIndex, child in pairs(character:GetChildren()) do
    	    if child:IsA("BasePart") and child.Name ~= "Head" then
       
        	child:GetPropertyChangedSignal("LocalTransparencyModifier"):Connect(function()
            	    child.LocalTransparencyModifier = child.Transparency
        	end)
       
        	child.LocalTransparencyModifier = child.Transparency
       
    	end
	end
 
	-- if the player steps in a vehicle
	camera:GetPropertyChangedSignal("CameraSubject"):Connect(function()
	    if camera.CameraSubject:IsA("VehicleSeat") then
 	       camera.CameraSubject = humanoid
    	    end
	end)
end

--// if they've already spawned
if player.Character then
	CharacterAdded(player.Character)
end

--// connect when they spawn
player.CharacterAdded:Connect(CharacterAdded)
4 Likes

Alright, I will try doing that too!

Oh, It worked. Thanks for the help!

1 Like

It’s nice, but I will continue using the other one, since the torso blocks most of the players sight

Oh you can easily ignore the torso by adding another conditional into the if statement to check that the child’s Name is not UpperTorso:

if child:IsA("BasePart") and child.Name ~= "Head" and child.Name ~= "UpperTorso"  then

I endorse the use of this script since it’s quite more efficient than updating the LocalTransparencyModifier every frame.