Hi! I’m here to ask for your help, more advanced programmers than me, with the following problem:
I’m making a tower defense type game, but there is a problem… when you reach Wave 4, until the last wave, the NPCs that spawn already start to lag, and move slowly, and get stuck… How could I optimize the code so that this does not happen again?
Handler(Script:serverscriptservice)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")
local Pathfinding = require(game.ServerScriptService.Pathfinding)
local RunService = game:GetService("RunService")
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()
for j = 2, #waypoints do
local path = Pathfinding:CreatePath(waypoints[j - 1], waypoints[j])
Pathfinding:MoveNPC(clonedZombie, path)
end
BindableEvent:Fire()
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)
Pathfinding(ModuleScript;serverscriptservice)
local PathfindingService = game:GetService("PathfindingService")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")
local BindableEvent = ReplicatedStorage:WaitForChild('NPCReachedEnd')
local Pathfinding = {}
local waypointsFolder = Workspace:WaitForChild("Waypoints")
local waypoints = {
waypointsFolder:WaitForChild("StartPart"),
waypointsFolder:WaitForChild("W2"),
waypointsFolder:WaitForChild("W3"),
waypointsFolder:WaitForChild("W4"),
waypointsFolder:WaitForChild("EndPart")
}
function Pathfinding:CreatePath(startPart, endPart)
local path = PathfindingService:CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = false,
AgentMaxSlope = 45
})
path:ComputeAsync(startPart.Position, endPart.Position)
return path
end
function Pathfinding:MoveNPC(npc, path)
local animationController = Instance.new("AnimationController")
animationController.Parent = npc
for _, waypoint in ipairs(path:GetWaypoints()) do
npc.Humanoid:MoveTo(waypoint.Position)
npc.Humanoid.MoveToFinished:Wait()
end
if npc.PrimaryPart.Position == waypoints[#waypoints].Position then
BindableEvent:Fire(npc)
end
end
return Pathfinding