Mass Npc pathfinding

I’m making a tower defense game, and so I need a large amount of npcs, the issue, the path is randomly generated, so I cant have waypoints built in, and so I need some sort of method to pathfind them all, I tried using a module with the function to create the path, which I then called from a script inside the mob, but after about 10-20 mobs spawn, they start stuttering, and eventually, stopping entirely. this makes it rather unfun, as after you hit a certain point, they literally don’t move at all.

so are there any suggestions on mass npc pathfinding?

I saw this thing about only rendering them when in view, but they didn’t show enough of the code for me to be able to actually implement it. (it was from crazyman32 in 2018)

Realized i should put the code

local function Path(Mob)
	local path = PFS:CreatePath(credentails)	
	local success, errorMessage = pcall(function()
		path:ComputeAsync(Mob:GetPivot().Position, workspace.Paths:WaitForChild("StarterTower", 5).Tower.Position)
	return path

		if v:FindFirstChild("Humanoid") then
				local human = v.Humanoid
				local path = Path(v)
				local waypoints = path:GetWaypoints()
				local nextWaypointIndex = 2
				local reachedConnection
				local blockedConnection
				if not reachedConnection then
					reachedConnection = human.MoveToFinished:Connect(function(reached)
						if reached and nextWaypointIndex < #waypoints then
							nextWaypointIndex += 1
				blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
					if blockedWaypointIndex >= nextWaypointIndex then
						path = Path(v)

I can run up to 150 npcs constantly moving with Pathfindingservice + moveto till i start to see any major fps drops, How are you handling the npcs actions?

i edited it so it has the code

task spawn isn’t needed as childadded will handle each new child separately (different thread not 100% sure im correct on technicality)
from the code you’ve shown every time “StartWave” event occurs it will run the childadded function for new npcs, so say you fire “StartWave” twice it’s now going to run that block of code twice, although even then you shouldn’t be seeing much lag unless you’re spamming startwave 50-100 times.

it can only be fired once all mobs are dead, and so i cant actually “spam waves” persay, so it only fires once, and I wasn’t sure if childadded created a new thread or not which is why the task.spawn

also, by stuttering I don’t mean lag or whatever, just that the movement becomes really laggy

yes but for every new “wave” that means a new function is going to be created in this case its “workspace.Enemies.ChildAdde:Connect(function()”
you should add a boolvalue via if myVar.Value == true the code below. You don’t need to fire a startwave event
Also i dont know where mobs are being created but just to make sure you’re doing :SetNetworkOwnership(nil) for the npcs?

I was doing it more for ease, but I’ll try doing it that way

I am not sure about task.spawn, but I know that spawn can have delays that can range to a few seconds on a large game. I recommend using coroutines and expecially corountine.wrap. Just replace task.wait with coroutine.wrap and call the function at the end as it returns a function that can then be called.

The difference in terms of format is small.


	print('Hello World!')


	print('Hello World!')

i think maybe the issue is that i’m keeping all the old connections from ChildAdded, and thats just slowing the server down a lot

memory only spikes up to 1500 lol

@VeIIlan it says :SetNetworkOwnership(nil) isn't a valid member of "model" are you sure thats what it is? sorry i’ve not toyed with network ownership yet.
just checked the wiki, its basepart only lol

for i,v in ipairs(model:getdescendants())

I managed to get it to not stop, by simply disconnecting the child added until I start another wave, but it’s still really stuttery. also i checked, and on the server they are stuttering too

I have managed to fix the issue by setting the WaypointSpacing to math.huge, making it so that it only puts points on turns, so now they only stutter when going around a corner, which just looks like they slowed down so they could turn better

1 Like