Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Attempt to index nil with "FindFirstChild" any solutions?

Notes ~ this is inside StarterPlayerScripts, which has an Integer Value named "Air", this is a local script inside of "Air"

Hey all, so I'm trying to make a game where you have a set amount of oxygen and when you run out it starts to damage you. So I made the first part work

local player = game.Players.LocalPlayer -- getting the player
local air = script.Parent -- the IntValue

while wait(1) do -- every one second
	if air.Value > 0 then -- check if any air is remaining
		air.Value = air.Value - 1 -- subtract an air
		print(air.Value.." air remaining.") -- print the air remaining
end

However, when I made some modifications to the script (see below), it stopped working and gave me this error

Players.v1rtical.PlayerScripts.Air.LocalScript:3: attempt to index nil with 'FindFirstChild'

Here is the new (broken) script

local player = game.Players.LocalPlayer
local air = script.Parent
local humanoid = player.Character:FindFirstChild("Humanoid")

while wait(1) do
	if air.Value > 0 then
		air.Value = air.Value - 1
		print(air.Value.." air remaining.")
	else
		print("No air remaining!")
		humanoid.Health = humanoid.Health - 10
	end
end

Hope someone can help, thanks!!

3 comments

0

change it to WaitForChild

0

change it to WaitForChild

0

I've done that and it gives me the same error but replaces "FindFirstChild" with "WaitForChild"

2 answers

✓ Accepted answer

This is because the Player's Character can sometimes take too long to load. Simply add a CharacterAdded:Wait() to yield the Character Object return until it is available.

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChildOfClass("Humanoid")

2 comments

0

This worked, but one more question, and this might be dumb, but I tried to do local health = Humanoid.Health and it hit me with the same error, except the nil value was health, any ideas?

0

Huh, I'm not sure about that one.

You are looking at the wrong part of the player. There are two instances that correspond to one player: a player instance that is located in game.Players that contains player scripts, etc, and a model of the player that appears in game.Workspace once you join the game, that contains physical aspects of the player such as the avatar parts and the Humanoid.

In order to find the Humanoid, you have to look for the model of the player in game.Workspace

local playerCharacter = game.Workspace:FindFirstChild(player.Name)

and then look for the Humanoid within the player model

local humanoid = playerCharacter:FindFirstChild("Humanoid")

3 comments

0

Hm, it's a good thought but I've input those variables and I'm still getting the same error

0

This is wrong

0

I'm unsure I have this problem where it'd say: Workspace.Admin Commands:2437: attempt to index nil with 'findFirstChild' It's like the person who started this thread.

This discussion is preserved as it was. New answers, comments, and votes are closed.