Need help with optimizing my code

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.

2 Likes

Omg,really appreciate it buddy!Thank you​:sob:you saved 2hours of my life​:sob::sob::sob:

1 Like

uh,there’s an error in modulescript: ‘Argument 1 missing or nil’
at:

npc.Humanoid:MoveTo(path[at])
1 Like

Try this:

npc.Humanoid:MoveTo(path[at].Position)

EDIT:
Also replace Pathfinding.StartMovingNPC(clonedZombie, waypoints) with Pathfinding.StartMovingNPC(clonedZombie, waypoints, 1) in line 42 in task.spawn function in handler script.

error: ServerScriptService.Pathfinding:7: attempt to index nil with ‘Position’

I found a better update for the code in modulescript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local BindableEvent = ReplicatedStorage:WaitForChild('NPCReachedEnd')

local Pathfinding = {}

function Pathfinding.StartMovingNPC(npc, waypoints, at)
     at = at or 2
     if at <= #waypoints then
          npc.Humanoid:MoveTo(waypoints[at].Position)
          npc.Humanoid.MoveToFinished:Once(function()
               Pathfinding.StartMovingNPC(npc, waypoints, at + 1)
          end)
     else
          BindableEvent:Fire(npc)
          npc:Destroy()
     end
end

return Pathfinding

This works really good

1 Like

Some more fixes to the script. I did some errors too, sry.

1 Like

Try the fixes I told in EDIT of above reply.

1 Like

Don’t worry! :sweat_smile:Is ok!I’m glad that you helped me,not that u made some little mistakes:)

Yeah,it works.Even if I put ‘Pathfinding.StartMovingNPC(clonedZombie, waypoints, 1)’ or i put ‘Pathfinding.StartMovingNPC(clonedZombie, waypoints, 2)’

Then just put 2 instead, will be more optimal solution for this.

@LakshyaK2011 There’s little more problem.From wave 5 above,the NPC when they arrive to a waypoint,they stay on that waypoint like 1 second,and then they move again…Why?

Even when they get destroyed,have a short delay

The code hasn’t been fully optimized yet, we still do task.spawn, I recommend removing that block completely and just call StartMovingNPC and Destroy method as they now don’t yield.

1 Like

actually,if i test the game in ‘run’ this problem doesn’t happen, but if I test the game as ‘play/play here’ then this problem happens

that’s really strange,i don’t know why this happens

Server to client replication delay, possibly due to that task.spawn function, I’ve said you to remove that already.

1 Like

Yeah, I did what you’ve been saying, it works pretty well now, thanks!

1 Like

If you want to optimize your NPCS then you should try having the NPCS just be tables on the server and render the NPCS on the client, i recommend you take a look at this post it helped me alot with my own NPC system

This is my first time posting on the devforum, i hope this helps :slightly_smiling_face:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.