Attempt to index nil with "Position" on NPC with Pathfinding

Hey, developers!

I’m trying to add Pathfinding to an already working NPC and I continue to get the error: "Workspace.Raptors.raptorAlpha.AttackPeople:105 attempt to index nil with "Position"

Here’s the code:
local PathFindingService = game:GetService("PathfindingService")

local aitorso = script.Parent:WaitForChild("Torso")

local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 2048
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end

function Hit(hit)
	local human = hit.Parent:FindFirstChild("Humanoid")
	if human ~= nil then
		human.Health =  human.Health -90
		wait(0.001)
		script.Parent.Head.Sound5:Stop()
		script.Parent.Head.Sound6:Stop()
		script.Parent.Head.Sound9:Stop()
		script.Parent.Head.Sound10:Stop()
		script.Parent.Head.Sound8:Play()
		script.Parent.Head.Sound2:Play()
		script.Parent.Concavenator.WalkSpeed = 0
		script.Parent.Torso.Neck.DesiredAngle = 0
		script.Parent.Head.JawJoint.DesiredAngle = 0
		wait(0.001)
		script.Parent.Torso.Neck.DesiredAngle = 0.1
		script.Parent.Head.JawJoint.DesiredAngle = 0.1
		wait(0.001)
		script.Parent.Torso.Neck.DesiredAngle = 0.2
		script.Parent.Head.JawJoint.DesiredAngle = 0.2
		wait(0.001)
		script.Parent.Torso.Neck.DesiredAngle = 0.3
		script.Parent.Head.JawJoint.DesiredAngle = 0.3
		wait(0.001)
		script.Parent.Torso.Neck.DesiredAngle = 0.4
		script.Parent.Head.JawJoint.DesiredAngle = 0.4
		wait(0.001)
		script.Parent.Torso.Neck.DesiredAngle = 0.5
		script.Parent.Head.JawJoint.DesiredAngle = 0.5
		wait(0.001)
		script.Parent.Torso.Neck.DesiredAngle = 0.7
		script.Parent.Head.JawJoint.DesiredAngle = 0.7
		wait(0.001)
		script.Parent.Torso.Neck.DesiredAngle = 0.9
		script.Parent.Head.JawJoint.DesiredAngle = 0.9
		wait(0.001)
		script.Parent.Torso.Neck.DesiredAngle = 1
		script.Parent.Head.JawJoint.DesiredAngle = 1
		wait(0.001)
		script.Parent.Torso.Neck.DesiredAngle = 0.9
		script.Parent.Head.JawJoint.DesiredAngle = 0.9
		wait(0.001)
		script.Parent.Torso.Neck.DesiredAngle = 0.7
		script.Parent.Head.JawJoint.DesiredAngle = 0.7
		wait(0.001)
		script.Parent.Torso.Neck.DesiredAngle = 0.5
		script.Parent.Head.JawJoint.DesiredAngle = 0.5
		wait(0.001)
		script.Parent.Torso.Neck.DesiredAngle = 0.4
		script.Parent.Head.JawJoint.DesiredAngle = 0.4
		wait(0.001)
		script.Parent.Torso.Neck.DesiredAngle = 0.3
		script.Parent.Head.JawJoint.DesiredAngle = 0.3
		wait(0.001)
		script.Parent.Torso.Neck.DesiredAngle = 0.2
		script.Parent.Head.JawJoint.DesiredAngle = 0.2
		wait(0.001)
		script.Parent.Torso.Neck.DesiredAngle = 0.1
		script.Parent.Head.JawJoint.DesiredAngle = 0.1
		wait(0.001)
		script.Parent.Torso.Neck.DesiredAngle = 0
		script.Parent.Head.JawJoint.DesiredAngle = 0
		script.Parent.Head.Sound7:Play()
		wait(0.7)
		script.Parent.Concavenator.WalkSpeed = 40
	end
end

larm.Touched:connect(Hit)
rarm.Touched:connect(Hit)

while true do
	local target = findNearestTorso(aitorso.Position)
	local path = PathFindingService:CreatePath()
	path:ComputeAsync(aitorso.Position, target.Position)
	local waypoints = path:GetWaypoints()

	for i, waypoint in pairs(waypoints) do
		if waypoint.Action == Enum.PathWaypointAction.Jump then
			script.Parent.Concavenator:ChangeState(Enum.HumanoidStateType.Jumping)
		end

		if target ~= nil then
			script.Parent.Concavenator:MoveTo(waypoint.Position)
		end
	end
end

The error occurs here:
path:ComputeAsync(aitorso.Position, target.Position)

I’m uncertain if the rest of the script works since this issue is stopping me from troubleshooting, but I’ll appreciate it if you can help!

1 Like

I have this problem too, and can’t fix it. Basically it’s saying that it can’t find a torso, therefore it’s nil.

1 Like

It’s weird because there most certainly is a child part called Torso, it wait’s for it yet doesn’t exist for some reason.