Attempt to index nil with 'Position'?
Asked by
4 years ago Edited 4 years ago
I'm basically just making an enemy npc that goes to the nearest npc in the game and attack each other. This script is located in the npc. The error is in line 29. Help please:
02 | local rot = script.Parent.HumanoidRootPart.Position |
03 | local animation = script:WaitForChild( "Animation" ) |
04 | local hold = script:WaitForChild( "Hold" ) |
05 | local human = script.Parent:WaitForChild( "Humanoid" ) |
06 | local ani = human:LoadAnimation(animation) |
07 | local anima 3 = human:LoadAnimation(hold) |
11 | function findNearestHumanoid(pos) |
12 | local list = game.Workspace:GetChildren() |
15 | if v.ClassName = = "Model" and v ~ = script.Parent then |
16 | local rootpart = v:FindFirstChild( "HumanoidRootPart" ) |
17 | local humanoid = v:FindFirstChildWhichIsA( "Humanoid" ) |
18 | if humanoid ~ = nil and rootpart and humanoid.Health < = 0 then |
19 | if (rootpart.Position - pos).Magnitude < dis then |
20 | local space = rootpart |
30 | local map = findNearestHumanoid(rot).Position - rot |
31 | local mag = map.Magnitude |
37 | local target = findNearestHumanoid(script.Parent.HumanoidRootPart.Position) |
39 | script.Parent:FindFirstChildWhichIsA( "Humanoid" ):MoveTo(target.Position) |
Thank you!