Why does it take so long for the AI to change its path

I made a zombie ai, but it changes it path so late whenever the player moves.

robloxapp-20200910-1429030.wmv (2.5 MB)

zombie script

while wait() do
	if zombieHum.Health <= 0 then
		zombieHum.Parent:ClearAllChildren()
	end
	local humRootPart = FindTarget()
	if humRootPart then
		local zombiePath = PFS:CreatePath()
		zombiePath:ComputeAsync(zombieTorso.Position, humRootPart.Position)
		
		local waypoints = zombiePath:GetWaypoints()
		
		for i, v in pairs(waypoints) do
			if waypoints.Action == Enum.PathWaypointAction.Jump then
				zombieHum:ChangeState(Enum.HumanoidStateType.Jumping)
			end
			zombieHum:MoveTo(v.Position)
			zombieHum.MoveToFinished:Wait()
		end
		zombieHum:MoveTo(humRootPart.Position)
	else
		wait(1)
		zombieHum:MoveTo(zombieTorso.Position + Vector3.new(math.random(-50,50), 0, math.random(-50,50)))
	end
end

Because before you recompute the path, you’re telling the zombie to finish its current path, no matter if its outdated.

2 Likes

I changed it but it still does the same thing

while wait() do
	local humRootPart = FindTarget()
	if humRootPart then
		local zombiePath = PFS:CreatePath()
		zombiePath:ComputeAsync(zombieTorso.Position, humRootPart.Position)
		
		local waypoints = zombiePath:GetWaypoints()
		
		for i, v in pairs(waypoints) do
			if waypoints.Action == Enum.PathWaypointAction.Jump then
				zombieHum:ChangeState(Enum.HumanoidStateType.Jumping)
			end
			zombieHum:MoveTo(v.Position)
			zombieHum.MoveToFinished:Wait()
		end
                                             -- removed line that was here
	else
		wait(1)
		zombieHum:MoveTo(zombieTorso.Position + Vector3.new(math.random(-50,50), 0, math.random(-50,50)))
	end
end

That line wasn’t the problem, the lines I quoted were.

You are looping through all the waypoints and calling MoveToFinished:Wait() for each one. The code doesn’t move to the next iteration of the while loop until all that work is done.