Attempt to index nil with "FindFirstChild"?

This is a checkpoint script for an obby, it works but always throws this error.


Full Script:

local checkpointDir = workspace.Checkpoints

function setupStats(plr)
local stats = Instance.new(“Folder”)
stats.Name = “leaderstats”
local stage = Instance.new(“NumberValue”)
stage.Name = “Stage”
stage.Value = 1
stats.Parent = plr
stage.Parent = stats
end

function AdvanceStage(plr,checkpoint)
local p = game.Players:GetPlayerFromCharacter(plr)
local stage = p:FindFirstChild(‘leaderstats’):FindFirstChild(‘Stage’)
if p and stage~=nil and plr:FindFirstChild(‘Humanoid’).Health>0 then
if(stage.Value+1) == tonumber(checkpoint) then
stage.Value = stage.Value+1
end
else
return
end
end

game.Players.PlayerAdded:connect(function(p)
setupStats(p)
p.CharacterAdded:connect(function(c)
local curStage = p:WaitForChild(‘leaderstats’):WaitForChild(‘Stage’)
if curStage~=nil and checkpointDir:WaitForChild(tostring(curStage.Value)) then
local point = checkpointDir:WaitForChild(tostring(curStage.Value))
wait(.3)
c:WaitForChild(‘HumanoidRootPart’).CFrame = CFrame.new(point.Position+Vector3.new(0,5,0))
end
end)
end)

for i,v in pairs(checkpointDir:GetChildren()) do
v.Touched:connect(function(h)
if h.Parent:WaitForChild(‘Humanoid’) then
AdvanceStage(h.Parent,v.Name)
end
end)
end

2 Likes

Try WaitForChild() vs FindFirstChild().
Use prints to make sure everything is working right …

1 Like

ouch … try printing what it’s looking for to see what is going on.
Prints are a great way to debug things.

Run the following on line 15 instead of the line that errors:

if not p then print("Player is nil!") return end
if not p:FindFirstChild("leaderstats") then print("leaderstats is nil!") return end

Tell us what it prints so we know how to help you further.

Or a print(p) … look over your structure in the explorer window … is everything where it should be?
Is everything spelled the same as the script … things like that.

this part: game.Players.PlayerAdded:connect(function(p)
looks like a server script so: stats.Parent = plr would not go to the right place …


Nothing has been printed

“Attempt to index nil with FindFirstChild” basically means nil:FindFirstChild() which is not desired.
As @NyrionDev stated, you can also do this instead:

local p = game.Players:GetPlayerFromCharacter(plr)
if not p then
    print("Player is nil!")
    return
else
    local stage = ...
end

When encountering occasions such as those where a variable could be nil, instead of directly indexing it, make sure it exists first!

Also, did you pass a Model to the function? That model should be a player character. If not, what part are you passing to the function? In case of it being a limb, you may index its parent.

The :GetPlayerFromCharacter() requieres a character not a player as an argument
so change the code to this :

--Function AdvanceToNextStage

local character = plr.Character or plr.CharacterAdded:Wait()
local p = game.Player:GetPlayerFromCharacter(character)

--Do The Rest
1 Like

In your function AdvanceStage(plr, checkpoint), you should realize that p can be nil and that completely ruins line 15.

Fix (line 15):

local stage = p and p:FindFirstChild('leaderstats'):FindFirstChild('Stage') or nil

unrelated but you should try to make your variables more clear, “p” should be renamed to “player” and “plr” to “character”

This error message literally means the variable you’re trying to index is nil. Backtrack your script and see what’s going wrong. If it’s meant to sometimes be nil, then return the script if so, or make a pcall() wraparound.