How to Move an Object with pathfinding service and lerping?

Hey people. I just start learning about the PathFinding Service and it seems to works decently well.
So anyway i need an idea on how would i move an object using pathfinding service and move them using Lerping
I refused using Humanoid:MoveTo() or the humanoid instance itself because its very badly optimized and will slow the game if there’s too much of it.

Here is my current Code of the pathfinding:

-- temp means target's torso
local path = pathfindingservice:CreatePath()
			path:ComputeAsync(root.Position,temp.Position)
			if path.Status == Enum.PathStatus.Success then
				local wp = path:GetWaypoints()
				local ogPos = temp.Position
				
				for i,point in pairs(wp) do
					if i == 1 then
						continue
					end					
				
					repeat 
						task.wait()
					until hum.FloorMaterial ~= Enum.Material.Air

					hum:MoveTo(point.Position)
					if temp then
						if (temp.Position - ogPos).Magnitude > 10 then
							break
						end
					else
						break
					end
					
					local finished = hum.MoveToFinished:Wait()
					if finished then
						break
					end
				end
				
				
			end

Is there any mistakes at my code I’d appreciate the help too!!

Why don’t you use the Pathfinding Module? I think it’s already perfect.

I’m trying to create my own codes but i might try it for a reference

There is nothing wrong with using open source modules
Almost all popular games use them

1 Like