Getting the error: "attempt to index nil with 'FindFirstChild'"

I’m getting this error but I can’t for the life of me figure out why it’s happening. Any help?
The error randomly happens when walking around inside the “SpawnBox”.
First image is the error itself, second image is the code the error refers to.


I would recommend using getpartsboundsinbox in a while loop instead for better hit detection because touched events can be quite inaccurate.

The problem might be with the spawnbox itself or the detected Could you show a video of the spawnbox and the error happening while you’re in it?

(A dirty method while bad practice is to just wrap the if statement in a pcall since it’s happening “randomly”, I don’t recommend since you wouldn’t really solve the problem you’d just prevent your script from breaking).

you are trying to call the FindFirstChild method of a value that is nil, so it says attempt to index nil

So hit.Parent is nil. Add hit.Parent to the check to see if it exists before using findfirstchild

2 Likes

Exactly.

OP, you need to check if hit is null to begin with in a separate if-statement. Example:

-- ...
if not hit then
    return
else if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name == Player.Name and not hit.Name == "Range" then
    -- ...
end
-...

And the same for the other event.

Additionally, using getpartsboundinbox is probably better as @Amritss said, and perhaps even collectionservice to tag eligible players for this. This should work as a simple fix, though you won’t want to keep this system in place for an extended period.

2 Likes

That’s not necessary if nil and ... is good enough. Short-circuit evaluation.

I thought this aswell but then I tested in studio myself and actually making a seperate if statement has no effect. Logically, it makes sense that you need to check if hit even exists to begin with and then start indexing in a nested if statement but no…

Problem is, hit shouldn’t even be nil to begin with. So even if you did get rid of the error, it’s not exactly solving the issue. I assume OP has deleted the detected basepart himself somewhere or it’s something to do with the hitbox. Not really sure there.

I thought OP already knew that “hit” is nil.. so restating it woudln’t be too helpful.

FindFirstChild("Humanoid")

This will be the Humanoid or nil, then you go on the same line to ask AND hit.Parent.Name.
If that comes back nil.. and it will on many parts before it hits one that will definitely have a Humanoid.. you’ll get that error for sure most of the time.

So split that up. If that is not nil, then go on with the next if statement.

local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid ~= nil then
    if hit.Parent.Name == "Something" then
        -- 
    end
end

Print hit and you’ll see why that happens.

2 Likes

Didn’t realize this was the case in lua! Good to know.

I tried multiple solutions here and they didnt work, however after some testing i figured out this error often happens right when i either tab out of studio or simply exit playtesting, not always though which is weird, maybe this will help in figuring out a solution somehow?

The answer was mentioned in Getting the error: "attempt to index nil with 'FindFirstChild'" - #3 by happya_x.

You are not checking if hit.Parent is nil. The error is explaining that you’re calling FindFirstChild on a nil value, and there’s only one place in your code that calls that method.

I don’t know why it’s nil, maybe something to do with streaming or replication. Regardless, you can gracefully nil check it by adding it to your if statement.

if hit and hit.Parent --[[nil checks hit.Parent]] and hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name == Player.Name then
	...

You might want to log using a warn when hit.Parent is nil, since this might be part of a larger problem in the code. Or maybe it’s just how destroying Instances works.

i tried adding a hit.Parent check but while it did remove the error message it kept the error’s effect like all code in that script just stopping. It might be something with StreamingEnabled tho, ill check

Your TouchEnded connection could probably make do without most of those checks, just check if hit.Name is in the table (are you supposed to be storing hit.Name or hit itself…? or hit.Parent.Name?).