Argument 1 missing or nil

while NPCSCanWalkValue.Value == true do

		if IsStunned == false then
			
			print("not stunned")
			local randomPart = PARTS[math.random(1, #PARTS)]
			local randomWait = math.random(20, 80) / 100
			print(tostring(randomPart).." Is the Part to Walk To")
			wait(randomWait)
			
			local Character = Waiter
			local success, errorMessage = pcall(function()
				path:ComputeAsync(Character.PrimaryPart.Position, randomPart)
			end)

			if success and path.Status == Enum.PathStatus.Success then
				waypoints = path:GetWaypoints()
			end
			
			Humanoid:MoveTo(waypoints.Position) -- Error: Argument 1 missing or nil
			
		else
			warn("Path not Computed")
		end
	end
	task.wait()
end -- There is some other code, that why there is an extra 'end'

I’ve been trying to fix this pathfinding code for a long time, and didn’t seem to get it to work because I’ve never worked with PathfindingService before.

Any help is appreciated!

Path:GetWaypoints() returns an Array of waypoints for the NPC to follow, Position doesnt exist within the Array so it is returned as nil which is why you are getting this error, you would have to get the index of the Waypoint and have the NPC follow it.

1 Like

How would I do that? Could you give an example?

Create a Variable for the index, so you can Basically keep track of the Waypoints, you would set the value to 2, like this:

-- outside functions
local nextindex

-- Within functions / loop
nextindex = 2
-- when following path:
Humanoid:MoveTo(waypoints[nextindex].Position) -- gets Position from index
nextindex += 1 -- adds a number so the NPC can move on to another waypoint

Obviously, this is a very basic Example.

1 Like
if IsStunned == false then
			
			print("not stunned")
			local randomPart = PARTS[math.random(1, #PARTS)]
			local randomWait = math.random(20, 80) / 100
			print(tostring(randomPart).." Is the Part to Walk To")
			wait(randomWait)
			
			local Character = Waiter
			local success, errorMessage = pcall(function()
				path:ComputeAsync(Character.PrimaryPart.Position, randomPart)
			end)

			if success and path.Status == Enum.PathStatus.Success then
				waypoints = path:GetWaypoints()
			end
			
			reachedConnection = Humanoid.MoveToFinished:Connect(function(reached)
				if reached and nextindex < #waypoints then
					nextindex += 1
					Humanoid:MoveTo(waypoints[nextindex].Position)
				else
					reachedConnection:Disconnect()
				end
			end)
			
			nextindex = 2
			Humanoid:MoveTo(waypoints[nextindex].Position) -- Error: attempt to index nil with number
			Humanoid.MoveToFinished:Wait()
			
		else
			warn("Path not Computed")
		end

(its all inside a while wait() loop)

Now I’m getting: “attempt to index nil with number” inside of the output. Why is this?

Is it because randomPart is used as #PARTS so that means it’s a number instead of a part?

I think it can be that you never actually stated the “nextindex” variable, so you should do

local nextindex = nil

At the start of the while loop, you should also delete the

nextindex = 2

since everytime you run it, it’s going to set the next waypoint to “2”. However if the “nil” variable doesn’t work then try replacing it with 1, the rest should be fine

1 Like

Try this! for i,waypoint in pairs(waypoints) do Humanoid:MoveTo(waypoint.Position) end

1 Like

When I do that it says: “attempt to index nil with nil”

Humanoid:MoveTo(waypoints[nextindex].Position) -- Error Here
Humanoid.MoveToFinished:Wait()
while NPCSCanWalkValue.Value == true do

	if IsStunned == false then
			
		local nextindex = nil
			
		print("not stunned")
		local randomPart = PARTS[math.random(1, #PARTS)]
		local randomWait = math.random(20, 80) / 100
		print(tostring(randomPart).." Is the Part to Walk To")
		wait(randomWait)
			
		local Character = Waiter
		local success, errorMessage = pcall(function()
			path:ComputeAsync(Character.PrimaryPart.Position, randomPart)
		end)

		if success and path.Status == Enum.PathStatus.Success then
			waypoints = path:GetWaypoints()
		end
			
		reachedConnection = Humanoid.MoveToFinished:Connect(function(reached)
			if reached and nextindex < #waypoints then
				nextindex += 1
				Humanoid:MoveTo(waypoints[nextindex].Position)
			else
				reachedConnection:Disconnect()
			end
		end)
			
		for i, waypoint in pairs(waypoints) do -- invalid argument #1 to 'pairs' (table expected, got nil)
			nextindex = 2
			Humanoid:MoveTo(waypoint.Position) 
			Humanoid.MoveToFinished:Wait()
		end
			
	else
		warn("Path not Computed")
	end
    task.wait()
end

The output says: “invalid argument #1 to ‘pairs’ (table expected, got nil)”

while NPCSCanWalkValue.Value == true do

		if IsStunned == false then
			
			local nextindex = 0
			
			print("Not Stunned")
			local randomPart = PARTS[math.random(1, #PARTS)]
			local randomWait = math.random(20, 80) / 100
			print(tostring(randomPart).." Is the Part to Walk To")
			wait(randomWait)
			
			local Character = Waiter
			
			path:ComputeAsync(Character.PrimaryPart.Position, randomPart.Position)
			print("Computed Path")
			
			local waypoints = path:GetWaypoints()
			nextindex += 1
			for i, waypoint in pairs(waypoints) do
				print("nextindex Set to: 1")
				Humanoid:MoveTo(waypoint.Position) 
				print("Moved")
				Humanoid.MoveToFinished:Wait()
			end
			
		else
			task.wait()
			warn("Stunned")
		end
	end
	task.wait()
end

Rearranging some stuff seemed to fix it, thank you for your guy’s help!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.