Really simple script not working?

My script is not working, I belive it is to do with the first person and third person detection.

While in first person fov is set to First_Person_Fov, vise versa for Third_Person_Fov. Besides the fact that while walking there is an added fov of 6.

I have spent 3 hours on figuring out why this does not work and gpt and claude cant do it right.

--Services
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

-- Variables
local Player = Players.LocalPlayer
local Camera = workspace.CurrentCamera
local Character = script.Parent
local Humanoid = Character:FindFirstChild("Humanoid")
local RootPart = Character:FindFirstChild("HumanoidRootPart")

-- Settings
local Third_Person_Fov = 70
local First_Person_Fov = 82
local Added_Fov = 6
local Tween_Time = 0.5 


-- State variables
local isWalking = false
local isInFirstPerson = false
local loop = true


-- Main

function Main()	
	
	if Humanoid.MoveDirection.Magnitude > 0 then
		isWalking = true

	else
		isWalking = false
	end

	
	if isInFirstPerson == true then
		Camera.FieldOfView = First_Person_Fov if isWalking == true then Camera.FieldOfView = First_Person_Fov + Added_Fov end
	else
		Camera.FieldOfView = Third_Person_Fov if isWalking == true then Camera.FieldOfView = Third_Person_Fov + Added_Fov end
	end
end


while loop == true do
	Main()
	task.wait(0.1)
end

This code looks to be using Python-specific features. It also has no code supporting the isInFirstPerson state

Screenshot 2024-12-25 192002

Basically what’s happening is that the humanoid variable is nil because of it not loading in fast enough before the script.

HumanoidRootPart and Humanoid instances both should have WaitForChild() to take time to load in the character.

Another thing too

the while loop you only need to make it like this for simplifying

while loop do
    Main()
    task.wait(0.1)
end

Also if you’re gonna detecting the player moving then we don’t need a constant loop detection rather then have running event.

--> Services
local TweenService = game:GetService("TweenService")
local WorkspaceService = game:GetService("Workspace")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

--> Variables
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local RootPart = Character:WaitForChild("HumanoidRootPart") :: Part
local Humanoid = Character:WaitForChild("Humanoid") :: Humanoid
local Camera = WorkspaceService.CurrentCamera

local isInFirstPerson = false
local Third_Person_Fov = 70
local First_Person_Fov = 82
local Added_Fov = 6

---------------------------------------------

local function UpdateFOV(speed: number)	
	local isWalking = (Humanoid.MoveDirection.Magnitude > 0)
	if isInFirstPerson then
		Camera.FieldOfView = First_Person_Fov
		if isWalking then
			Camera.FieldOfView = First_Person_Fov + Added_Fov
		end
	else
		Camera.FieldOfView = Third_Person_Fov
		if isWalking then
			Camera.FieldOfView = Third_Person_Fov + Added_Fov
		end
	end
end

Humanoid.Running:Connect(UpdateFOV)

Also don’t forget to make the character variable also wait too.

1 Like