Attempt to Index nil with 'Position'

I am writing a code for the NPC to follow and potentially attack the player, but an error appears. In the output, it says “Attempt to Index nil with ‘Position’”
Here’s my script and the output:

I used print(“E”) function to identify errors, and it seems to get stuck before the 2nd if statement. If I remove the or in the first if statement and replace it with and, the script loops a lot between lines 9 and 12.

Anyone know how to fix this issue?

You have it as “or” statements. Make it this:

if v ~= script.Parent and torso and human then

If I do, the NPC does nothing, and the script loops between lines 9 and 12, which is between the variables human and torso
The Loop lasts for a little bit then continues the script, but nothing happens to the NPC

Yes, but that just means whatever the loop went through does not have a Humanoid or a HumanoidRootPart.

Oh ok. It fixed it thanks. Also, the NPC wasn’t moving cause the HumanoidRootPart of the zombie NPC was anchored.

1 Like