Is this NPC unoptimized? help needed

So, I just made an NPC that roams the map, and I wanted to know if I could do this better. the main thing I want help with is this:

local List = {}
	for _, Part in pairs(script.Paths.Value:GetChildren()) do
		if Part:IsA("BasePart") and (Part.Position - humanoid.RootPart.Position).Magnitude < 200 then
			table.insert(List,Part)
		end
	end
	if #List > 0 then
		CurrentDestination = List[math.random(1,#List)]
	else
		warn("an NPC failed to find a path")
		Character:Destroy()
	end

there are about 200 hundred locations. all they do is simply walk to it.
here is the code for walking to it:

while wait() do 
	local target = findTarget()
	CDMGCD += .1
	if humanoid.Health == 0 then
		break
	end
	if target and GlobalValues.IsDay.Value == false then
		humanoid.WalkSpeed = 16
		followPath(target.HumanoidRootPart.Position)
	else
		humanoid.WalkSpeed = 10
		if CurrentDestination == nil then
			GetCurrentDestination()
		end
		if (CurrentDestination.Position - humanoid.RootPart.Position).Magnitude < 5 then
			GetCurrentDestination()
		end
		followPath(CurrentDestination.Position)
	end
end

the followPath function is just a simple pathfind

there is also about 15 npc in the map at a time

the wait() is pretty unoptimized and got replaced by task.wait() which is 2x accurate.

explanation here

anyway, i dont recommend to use a while loop, instead, use game.Runservice.RenderStepped

it will run every frame after the physic got simulated first, basically is what you need
(dont worry, it doesnt lag)

by the way, you did a good job

well, I need it to run .1 second specifically.

local target = findTarget()
	CDMGCD += .1
	LastHit += .1
	SinceGoodPath -= .1
	if humanoid.Health == 0 then
		break
	end
	if target and GlobalValues.IsDay.Value == false then
		humanoid.WalkSpeed = 16
		followPath(target.HumanoidRootPart.Position)
	else
		humanoid.WalkSpeed = 10
		if CurrentDestination == nil then
			GetCurrentDestination()
		end
		if (CurrentDestination.Position - humanoid.RootPart.Position).Magnitude < 5 or SinceGoodPath <= 0 then
			GetCurrentDestination()
			SinceGoodPath = 20
		end
		local p = followPath(CurrentDestination.Position)
		if p == false then
			print("Forced Move")
			GetCurrentDestination()
			followPath(CurrentDestination.Position)
		end
	end
	if LastHit > 5 then
		nextWaypointIndex += 1
		LastHit = 0
		print("Hit Reset")
	end

I’m having some big issues right now with them getting stuck

Also while loops on server are better than performing stuff every frame, you don’t need soo much precision, make calculation every 10-15 frames

Apparently my game or npcs ain’t optimized enough, cause I did a test with my friends and the server took about 5 seconds to receive an event.

can you describe a bit more whats happening?

if a single npc is just walking around, and you’re using a path finding function to direct the npc
this in and of itself isn’t really taxing on the server

now having a lot of npcs is a different kinda problem
in this case of having a lot of npcs, your path finding function isn’t being unoptimal, its the npc’s humanoids themselves (iirc)
if you’re trying to have a ton of npcs i recommend watching this video by suphi kaner: https://www.youtube.com/watch?v=UamdKI7k4Mw&t=21055s, its a journey but a very insightful one

what does this mean?
where does an event tie into this?

if you’re worried about just the server replicating the current position of the npc then there isn’t really much you can do
have you taken latency into account?

There is a building system that is extremely delayed, because it’s controlled by an event.
(The system is turned on and off with an event)

I will watch that video when I am not occupied. I am busy at the moment.