Path finding being slow

Hi! I need some help with my pathfinding script. It works pretty fine, but when there are a lot of NPCs using the same pathfidning script it starts to work slow. Here’s a video:

I would appreciate any help!

2 Likes

Have you tried setting the NetworkOwnership to the server? I’ve seen that from videos may help.

May you show us the script? We can try to make it more efficient for you!

Are you using a new coroutine for each of the NPC’s?

If not then the NPC’s will stop as the script calculates the next position for a different NPC. With coroutines the code will be able to be run at the same time for all of the NPC’s.

But as has already been stated, seeing the code would help a lot.

while true do
	wait()
	local target = script.Parent.Target.Value
	local EndPath = false
	if target then
		local hit, pos = raycast(script.Parent.Torso.Position,(target.Position-script.Parent.Torso.Position).unit,999)
		if hit and hit.Parent~=nil then
			local h=hit.Parent:FindFirstChild("Humanoid")
			if h and script.Parent.Torso.Position.Y > (script.Parent.Torso.Position.Y - 2) then
				EndPath = true
			end
		end
		if EndPath == false then
			script.Parent.Attack.Value = false
			local path = game:GetService("PathfindingService"):CreatePath(pathArgs)
			path:ComputeAsync(script.Parent.Torso.Position, target.Position)
			local waypoints = path:GetWaypoints()
			for i, waypoint in pairs(waypoints) do
				if waypoint.Action == Enum.PathWaypointAction.Jump then
					script.Parent.Humanoid.Jump = true
				end
				if target then
					if path.Status == Enum.PathStatus.Success then
						local points = path:GetPointCoordinates()
						if #points < 3 then
							script.Parent.Humanoid:MoveTo(script.Parent.Target.Value.Position)
						else
							script.Parent.Humanoid:MoveTo(points[3])
						end
					else
						script.Parent.Humanoid:MoveTo(script.Parent.Target.Value.Position)
					end
				end
				
			end
		else
			script.Parent.Humanoid:MoveTo(script.Parent.Target.Value.Position)
			script.Parent.Attack.Value = true
		end
	end
end

The problem with your code is that you are path finding about 20 times per second, which uses A LOT, I mean A LOT of computing power, considering the fact that you have a lot of NPCs. Considering using:

humanoid.MoveToFinished:Wait()

As this will wait until the NPC finished the path.

1 Like

This happens, I added:

script.Parent.Humanoid.MovedToFinished:Wait()

Below the:

script.Parent.Humanoid:MoveTo(position)

May you copy and paste this code into your code?

for i, waypoint in pairs(waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
	script.Parent.Humanoid.Jump = true
end
if target then
	if path.Status == Enum.PathStatus.Success then
		local points = path:GetPointCoordinates()
		if #points < 3 then
			script.Parent.Humanoid:MoveTo(script.Parent.Target.Value.Position)
		else
			script.Parent.Humanoid:MoveTo(points[3])
		end
	else
		script.Parent.Humanoid:MoveTo(script.Parent.Target.Value.Position)
	end
    print("a")
end

And may you record a video of the output?

aecf4e5f6d575798cc0136a6cc220698
My computer almost dies ngl.
The path finding works pretty good, and it just gets cluncky when a big ammount of npcs are path finding.

May you try recording a video, the purpose is to try to see how fast it prints. Try to use 1 npc. And is this script located in a for loop? If it is, consider using coroutine.

Yeah! I used coroutines and it worked! thanks.