Why my script get the error attempt to index nil with 'FindFirstChild'

How i can fix attempt to index nil?

dev console message
ServerScriptService.Scripts.CheckpointScript:15: attempt to index nil with ‘FindFirstChild’ - Server - CheckpointScript:15
CheckpointScript

game.Players.PlayerAdded:Connect(function(Player)
	local Stage = Instance.new("NumberValue")
	Stage.Parent = Player
	Stage.Name = "Stage"
	Stage.Value = 0
	
	local CheckpointsFolder = game.Workspace:WaitForChild("Checkpoints")
	
	for i, Checkpoint in pairs(CheckpointsFolder:GetChildren()) do
		
		Checkpoint.Touched:Connect(function(Hit)
			if Hit.Parent:FindFirstChild("Humanoid") then
				local PlayerHit = game.Players:GetPlayerFromCharacter(Hit.Parent)
				
				if PlayerHit:FindFirstChild("Stage").Value == Checkpoint.Name - 1 then
					PlayerHit:FindFirstChild("Stage").Value = Checkpoint.Name
					
				end
			end
		end)
		Player.CharacterAdded:Connect(function(Character)
			repeat
				task.wait()
			until Character ~= nil 
			local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
			HumanoidRootPart.CFrame = CheckpointsFolder:FindFirstChild(Stage.Value).CFrame + Vector3.new(0, 2, 0)
		end)
	end
end)
1 Like

You should check if the player exists first, then you can run the following code after.

Fixed Code (around Line 15):

if Hit.Parent:FindFirstChild("Humanoid") then
	local PlayerHit = game.Players:GetPlayerFromCharacter(Hit.Parent)
	
	if PlayerHit then -- here we check if player exists		
		if PlayerHit:FindFirstChild("Stage").Value == Checkpoint.Name then
			PlayerHit:FindFirstChild("Stage").Value = Checkpoint.Name
		end			
	end	
end
1 Like

this dont worked how i can fix?

1 Like

Was there any errors in the output when you tried using the code I gave?

1 Like

12:01:26.491 ServerScriptService.Scripts.CheckpointScript:20: Expected identifier when parsing expression, got ‘)’

1 Like

Could you show your current code so I can see whats going on?

1 Like

yes

game.Players.PlayerAdded:Connect(function(Player)
	local Stage = Instance.new("NumberValue")
	Stage.Parent = Player
	Stage.Name = "Stage"
	Stage.Value = 0
	
	local CheckpointsFolder = game.Workspace:WaitForChild("Checkpoints")
	
	for i, Checkpoint in pairs(CheckpointsFolder:GetChildren()) do
		
		Checkpoint.Touched:Connect(function(Hit)
			if Hit.Parent:FindFirstChild("Humanoid") then
				local PlayerHit = game.Players:GetPlayerFromCharacter(Hit.Parent)
				
              if PlayerHit then
				
				if PlayerHit:FindFirstChild("Stage").Value == Checkpoint.Name - 1 then
					PlayerHit:FindFirstChild("Stage").Value = Checkpoint.Name
					
				end
			end
		end)
		Player.CharacterAdded:Connect(function(Character)
			repeat
				task.wait()
			until Character ~= nil 
			local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
			HumanoidRootPart.CFrame = CheckpointsFolder:FindFirstChild(Stage.Value).CFrame + Vector3.new(0, 2, 0)
		end)
	end
end)
1 Like

You forgot to add another end, I believe thats why you are getting this error.
Here is your fixed code:

game.Players.PlayerAdded:Connect(function(Player)
	local Stage = Instance.new("NumberValue")
	Stage.Parent = Player
	Stage.Name = "Stage"
	Stage.Value = 0
	
	local CheckpointsFolder = game.Workspace:WaitForChild("Checkpoints")
	
	for i, Checkpoint in pairs(CheckpointsFolder:GetChildren()) do
		
		Checkpoint.Touched:Connect(function(Hit)
			if Hit.Parent:FindFirstChild("Humanoid") then
				local PlayerHit = game.Players:GetPlayerFromCharacter(Hit.Parent)
				
              if PlayerHit then
				
				if PlayerHit:FindFirstChild("Stage").Value == Checkpoint.Name - 1 then
					PlayerHit:FindFirstChild("Stage").Value = Checkpoint.Name
					end
				end
			end
		end)
		Player.CharacterAdded:Connect(function(Character)
			repeat
				task.wait()
			until Character ~= nil 
			local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
			HumanoidRootPart.CFrame = CheckpointsFolder:FindFirstChild(Stage.Value).CFrame + Vector3.new(0, 2, 0)
		end)
	end
end)
1 Like

the checkpoint script worked thank you :wink:

1 Like

You are welcome! Happy to help!

1 Like