Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Attempt to index nil with "FindFirstChild" any solutions?

Asked by 4 years ago

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!!

0
change it to WaitForChild greatneil80 2647 — 4y
0
change it to WaitForChild greatneil80 2647 — 4y
0
I've done that and it gives me the same error but replaces "FindFirstChild" with "WaitForChild" xIshFudge 84 — 4y

2 answers

Log in to vote
1
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago

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")
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? xIshFudge 84 — 4y
0
Huh, I'm not sure about that one. Ziffixture 6913 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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")

0
Hm, it's a good thought but I've input those variables and I'm still getting the same error xIshFudge 84 — 4y
0
This is wrong Ziffixture 6913 — 4y
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. Kearna09 -5 — 4y

Answer this question