How can i fix this patchfinding bahavior?

Basically, every waypoint he goes and stop and goes again just stop which make the move not right for the following npcs. Any way i can easily fix it?

local patchfindingservice = game:GetService("PathfindingService")

local player = false

local success,patch = pcall(function()
	return patchfindingservice:CreatePath(
		{
			AgentRadius = 2,
			AgentHeight = 5,
			AgentCanJump = true,
			AgentCanClimb = true,
			WaypointSpacing = 6,
		}
	)
end)

if not success then
	print("Could not create path! :(", patch)
	script.Parent:Destroy()
	return
end

local waypointsuccess,errorm = pcall(function()
	patch:ComputeAsync(script.Parent.HumanoidRootPart.Position,workspace.AILookPoints.itsMuneeb.Stand.Position)
end)

if success and patch.Status == Enum.PathStatus.Success then
	local waypoints = patch:GetWaypoints()
	local hum = script.Parent:WaitForChild("Humanoid")

	for i,v in pairs(waypoints) do
		if v.Action == Enum.PathWaypointAction.Jump then
			hum.Jump = true
		end

		hum:MoveTo(v.Position)
		hum.MoveToFinished:Wait()
	end

	game:GetService("Chat"):Chat(script.Parent.Head, "Hello, i would like to order a mac nd cheeze", Enum.ChatColor.Blue)

	task.wait(3)
	game:GetService("Chat"):Chat(script.Parent.Head, "Also i would take a Bloxy Cola", Enum.ChatColor.Blue)
end
1 Like

Closer view of the issue

for i,v in pairs(waypoints) do
		if v.Action == Enum.PathWaypointAction.Jump then
			hum.Jump = true
		end

		hum:MoveTo(v.Position)
		hum.MoveToFinished:Wait()
	end

It could be the wait function, maybe you can detect when the HumanoidRootPart’s magnitude is close to the waypoint (3-5 studs away) then make them MoveTo the next position e.t.c This way you are prefiring the next calculation before they actually hit the waypoint?

I’m not sure if this will entirely work but in theory it does sound like it will work.

1 Like

I need something a bit precise since i don’t want npc to go through whalls but also not doing that weird behavior.

The npc shouldn’t go through the wall or attempt to go through it PathfindingService states this on the documentation

PathfindingService is used to find logical paths between two points, ensuring that characters can move between the points without running into walls or other obstacles. By default, the shortest path is calculated, but you can implement pathfinding modifiers to compute smarter paths across various materials, around defined regions, or through obstacles.

So by calculating the path early you prevent it from making that awkward stop. The :Wait() is most likely the cause of it. I would personally take the WaypointSpacing = 6 then start your calculation when the humanoid is WaypointSpacingVar / 2.25 or 6/2.25

Someone else who may be more experienced in PathfindingService can probably tell you a better solution if one exists.

Perhaps it’s the network ownership? I’ve seen some path finding tutorials before and they set the NPC’s network owner to the server.

I believe this is because having its network owner set to a player might cause the NPC to lag in the server, thus delaying MoveToFinished but feel free to correct me.

I’ve tried this with one of my games and my NPCs seem to move very fine.

2 Likes

Lemme give it a try and see if it fix the issue. Will let you updated.

Try replacing hum:MoveTo(v.Position)
with
hum:Move(v.Position-hrp.Position) (hrp = the character’s HumanoidRootPart, or alternatively char:GetPivot().Position)

Move works like MoveTo but instead of getting passed a position, it gets a direction and moves the humanoid’s character in that direction. Not sure how MoveToFinished works with that, though. You could use some built-in function of pathfinding service that checks when the next waypoint is reached, and yield the for … in waypoints loop until it’s reached, or manually check the character’s distance to the current waypoint to continue iterating.

Hey there! it actually did fix the problem by setting the HumanoidRootPart ownership to nil and also prevent exploiters to kill the npcs.

Thanks for the solution!

1 Like