AI doesnt go to 2nd Block

I’m trying to make an NPC repeat a loop that allows it to go from one part to another. It goes to the first part and then doesn’t do anything else. Any help greatly appreciated!

local NPC = script.Parent
local humanoid = NPC.Humanoid
local destination = workspace.destination
local pathfindingService = game:GetService("PathfindingService")



local function getPath(destination)
	local path = pathfindingService:CreatePath()
	path:ComputeAsync(NPC.Torso.Position, workspace.destination.Position)
	
	return path
end


local function walkTo(destination)
	local path = getPath(destination)
	
	for index, waypoint in pairs(path:GetWaypoints())do
	humanoid:MoveTo(waypoint.Position)
	humanoid.MoveToFinished:Wait()
		
	end
end
		
while true do
	walkTo(workspace.destination)
	walkTo(workspace.destination2)
	
end


It seems like your .MoveToFinished:Wait() is never occuring. I would suggest having a custom function which checks the distance between the destination node and the AI, that way every couple of milliseconds it checks the distance and if it’s within an acceptable radius, you would break the function. This type of function would give you more control over your AI functionality.

To check if the second walkTo() call is occuring, add a print statement in between the calls to verify they are running.

2 Likes

Any path made using PathfindingService has limits, so it may not be able to navigate through complex maps and such. Try widening doorways/narrow paths, and see if that works.

2 Likes

It is the default baseplate with two parts.

1 Like

Ah, forgive me. I assumed you had a map, whoops! :sweat_smile: Do you have any errors in your Output window?

1 Like

Nope, sadly. I really don’t know what is happening.

1 Like

They both print in the output after trying that.

Try adding a wait(1) before calling walkTo() again for the second part

still wont budge sadly (30char30char)

Try posting a barebones .rbxl file version which includes the code and the NPC so we can try and fix it for you, then tell you what we did.

NPC DEVFORUM.rbxl (29.2 KB) NPC DEVFORUM.rbxl (29.2 KB)

Ah, I’ve spotted the error:

local function getPath(destination)
    path:ComputeAsync(NPC.Torso.Position, workspace.destination.Position)

You should replace workspace.destination.Position with destination.Position

1 Like

@EmeraldLimes is correct.

I’ve also implemented a custom distance checker algorithm for you in case you want to switch to it in the future.

NPC DEVFORUM (1).rbxl (29.5 KB)

1 Like

So I defined a variable for that and forgot to use it?

1 Like

Yep! Happens all the time to me too :sweat_smile:

1 Like