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)
end)
return path
end
game.ReplicatedStorage.Events.StartWave.Event:Connect(function()
workspace.Enemies.ChildAdded:Connect(function(v)
if v:FindFirstChild("Humanoid") then
task.spawn(function()
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
human:MoveTo(waypoints[nextWaypointIndex].Position)
else
reachedConnection:Disconnect()
end
end)
end
blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
if blockedWaypointIndex >= nextWaypointIndex then
blockedConnection:Disconnect()
path = Path(v)
end
end)
human:MoveTo(waypoints[nextWaypointIndex].Position)
end)
end
end)
end)
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?
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
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 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.
@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
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