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.
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.
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
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
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!