Pathfinding gets stupid when i don't use Humanoid.MoveToFinished:Wait()

So basically im working on a pathfinding system using the new PathFindingService.

If i add Humanoid.MoveToFinished:Wait() after using Humanoid:MoveTo(waypoint) it works but if i move to another direction NPC doesn’t changes his direction and just keep going on his path so this makes a laggy look.

robloxapp-20240801-1425419.wmv (1.9 MB)

If i don’t add Humanoid.MoveToFinished:Wait() after the Humanoid:MoveTo(waypoint) it is smoother but this time it doesn’t listens to pathfinding parameters such as climbing, costs etc.

robloxapp-20240801-1426247.wmv (2.5 MB)

Here is the code:

local function FollowPath()
						local path = pFS:CreatePath(
							{
								AgentHeight = 5;
								AgentRadius = 2;
								AgentCanJump = true;
								AgentCanClimb = true;
								Costs = {
									DangerZone = math.huge;
								}
							})
						local success, errorMessage = pcall(function()
							path:ComputeAsync(character.PrimaryPart.Position, enemyRoot.Position)
						end)

						local nextWaypointIndex, reachedConnection, blockedConnection
						if success and path.Status == Enum.PathStatus.Success then
							local waypoints = path:GetWaypoints()
							local startTime = tick()

							for ind, wp in pairs(waypoints) do
								if ind ~= 1 then
									selfHum:MoveTo(wp.Position)
									selfHum.MoveToFinished:Wait()

									if wp.Action == Enum.PathWaypointAction.Jump then
										selfHum:ChangeState(Enum.HumanoidStateType.Jumping)
									end
								end
							end 
						else
							warn("failed")
							FollowPath()
						end
					end
					FollowPath()

2 Likes

That function is to ensure that the NpC completes that task before doing that task,

So thats why it wont work because The Npc would have to complete that task before doing changing direction

If do not have that,it do not know which one to start

1 Like

So what should i do?

> character limit

local Stop = 0
local function FollowPath()
                                                Stop += 1
                                                local myStop = Stop
						local path = pFS:CreatePath(
							{
								AgentHeight = 5;
								AgentRadius = 2;
								AgentCanJump = true;
								AgentCanClimb = true;
								Costs = {
									DangerZone = math.huge;
								}
							})
						local success, errorMessage = pcall(function()
							path:ComputeAsync(character.PrimaryPart.Position, enemyRoot.Position)
						end)

						local nextWaypointIndex, reachedConnection, blockedConnection
						if success and path.Status == Enum.PathStatus.Success then
							local waypoints = path:GetWaypoints()
							local startTime = tick()

							for ind, wp in pairs(waypoints) do
                                                                if myStop ~= Stop then break end
								if ind ~= 1 then
									selfHum:MoveTo(wp.Position)
									selfHum.MoveToFinished:Wait()

									if wp.Action == Enum.PathWaypointAction.Jump then
										selfHum:ChangeState(Enum.HumanoidStateType.Jumping)
									end
								end
							end 
						else
							warn("failed")
							FollowPath()
						end
					end
end

while true do
    task.wait(0.1)
    task.spawn(FollowPath)
end

Create a string value (Put it either in workspace or replicatedStroage).

selfhum.Humanoid.WalkSpeed = 0 – in line (if w == p.Action)