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

Attempt to index nil with "FindFirstChildOfClass"?

Asked by
naturedat 124
3 years ago

This is just a damage script I put in my AI. I'm not sure why but when I hit them with a sword, there is no error but error appeared when I shot them with a bow. I guess that they computer might thought that I mean it differently because I clone an arrow into my tool when I shoot.

local cool = false
local slash = script:WaitForChild("Slash")
local human = script.Parent.Parent:FindFirstChildWhichIsA("Humanoid")
local anim2 = human:LoadAnimation(slash)
script.Parent.Touched:Connect(function(hit)
    if cool == false and script ~= nil then
        if hit.Parent:FindFirstChildOfClass("Humanoid") then -- error here
            local enemy = hit.Parent:FindFirstChildOfClass("Humanoid")
            if hit.Parent ~= script.Parent.Parent and enemy.Name ~= human.Name then
                cool = true
                anim2:Play()
                enemy.Health = enemy.Health - 15
                wait(2)
                cool = false
            end
        end
    end
end)

Help please. Thankyou.

0
To diagnose, try printing the "hit" object. Does "hit" return anything? Like a name? Y_VRN 246 — 3y
0
Yes I did, but they said a there's a new error, attempt to index nil with "Name" on the line that I wrote print(hit.Parent.Name) naturedat 124 — 3y
0
Urgh, sometimes the error just appear and other times it doesn't, this is annoying. naturedat 124 — 3y
0
Can you do print(hit) instead? It'll tell if it says "This object is not a valid member of <This object>." Y_VRN 246 — 3y
0
Yep, I just tried that and it did print the object name with no error naturedat 124 — 3y

2 answers

Log in to vote
1
Answered by
Y_VRN 246 Moderation Voter
3 years ago
Edited 3 years ago

I might've found the answer:

I theorize that you parented the arrow projectile to the player's character, programmed to remove after touching something, and then the AI throws an error because it did not catch up with the disappearance of the projectile, thinking it is nil because it fired after the arrow is gone?

Maybe try a different approach to this as I don't recommend using this method since it comes with cons on multiplayer games, most notably when the AI touches the player or vice versa because it'll look for an object that belongs to players, and theplayer's body parts are considered.

I have not made a game with weapons so I might be not much of help on this genre, but if you just want the AI to be hurt when an arrow gets hit, and that you're using the default tool system, consider using the projectile instead for damage, like when the projectile hits something, check if it is an AI or something else. If it is an AI, use the humanoid reference and deduct with Humanoid:TakeDamage(&lt;amountofdamage>), otherwise ignore. After the touch, remove the projectile.

Hope this helps even if I may have not explained well.

0
Actually, I have parented the projectile itself to the tool, I had made the projectile wait(0.1) before destroying itself after touching an object. But there was still that error, appearing sometimes. I suppose this is a game error so I guess I'm just going to leave it there and hopefully found the solution in the future. naturedat 124 — 3y
0
Thanks for trying to help naturedat 124 — 3y
0
Actually, it worked!!!, the waiting did the job. Thanks! naturedat 124 — 3y
0
You're welcome! :D Sorry for the late reply though! Y_VRN 246 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

"hit" is usually the part that hit it. If the hitpart, otherwise, the part that hit, has no instance under its parent that's called "Humanoid", the value will return nil and thus it will error. >hit.Parent:FindFirstChildOfClass("Humanoid") only works for players that touched the script's Parent.

0
Correct me if I'm wrong, but I think the error is suggesting that the value returned by "hit" is nil and trying to run the FindFirstCh... function won't work because nil is empty and does not contain any functions, or that the function is simply absent. If it does then the error would be avoided because the if statement knows it is nil, and so it will not hurt a non-living item. Y_VRN 246 — 3y
0
I'm not sure but it might be the game's error, I did thought of it could be nil, that's why I put the if statement there. So yeah, the game was trying to find a humanoid in my arrow even though I use the if statement. naturedat 124 — 3y

Answer this question