Optimised Handler script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Pathfinding = require(game.ServerScriptService.Pathfinding)
local BindableEvent = ReplicatedStorage:WaitForChild("NPCReachedEnd")
local RemoteEvent = ReplicatedStorage:WaitForChild('WaveUpdate')
local waypointsFolder = workspace:WaitForChild("Waypoints")
local enemiesFolder = ReplicatedStorage:WaitForChild("Enemies")
local waypoints = {
waypointsFolder:WaitForChild("StartPart"),
waypointsFolder:WaitForChild("W2"),
waypointsFolder:WaitForChild("W3"),
waypointsFolder:WaitForChild("W4"),
waypointsFolder:WaitForChild("EndPart")
}
local waves = {
{ BasicZoombie = 3 },
{ BasicZoombie = 5 },
{ BasicZoombie = 6, Zoombie = 1 },
{ BasicZoombie = 5, Zoombie = 3 },
{ Zoombie = 5, RareZoombie = 2 },
{ Zoombie = 6, RareZoombie = 5, GoldZoombie = 2 },
{ RareZoombie = 8, GoldZoombie = 6 },
{ RareZoombie = 9, GoldZoombie = 8, BasicZoombie = 12, Zoombie = 4 },
{ StrongZoombie = 1, GoldZoombie = 2 },
{ StrongZoombie = 2, Zoombie = 9 }
}
local activeNPCs = 0
local currentWave = 1
local function spawnNPC(npcType)
local npcTemplate = enemiesFolder:FindFirstChild(npcType)
if npcTemplate then
local clonedZombie = npcTemplate:Clone()
clonedZombie.Parent = workspace
clonedZombie:SetPrimaryPartCFrame(waypoints[1].CFrame)
task.spawn(function()
Pathfinding.StartMovingNPC(clonedZombie, waypoints)
clonedZombie:Destroy()
end)
end
end
local function startWave(waveNumber)
RemoteEvent:FireAllClients(waveNumber)
local waveData = waves[waveNumber]
activeNPCs = 0
for npcType, count in pairs(waveData) do
for i = 1, count do
activeNPCs = activeNPCs + 1
spawnNPC(npcType)
wait(2)
end
end
end
BindableEvent.Event:Connect(function()
activeNPCs = activeNPCs - 1
if activeNPCs == 0 then
currentWave = currentWave + 1
if currentWave <= #waves then
wait(4)
startWave(currentWave)
end
end
end)
task.wait(.5)
startWave(currentWave)
Optimized Pathfinding:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local BindableEvent = ReplicatedStorage:WaitForChild('NPCReachedEnd')
local Pathfinding = {}
function Pathfinding.StartMovingNPC(npc, path, at: number)
local animationController = Instance.new("AnimationController")
animationController.Parent = npc
npc.Humanoid:MoveTo(path[at])
if(at <= #path) then
npc.Humanoid.MoveToFinished:Once(function(ignored) Pathfinding.StartMovingNPC(npc, path, at+1) end)
else
BindableEvent:Fire(npc)
end
end
return Pathfinding
You may wanna rename Pathfinding to NPC Mover or something like that.