Any other ways to optimize this NPC Script?

I Made a quick NPC Module that runs on the Client that i plan to use in my other projects. the NPC Uses TweenService instead of the normal Humanoid:MoveTo()
i wanna know if you guys think this script is optimized enough, i got ~150 FPS with 300 NPCs & ~40 - 50 FPS with 1000.

.
the NPC itself is already pretty optimized, R6, No Humanoid, Animate Script, Collision, i even changed the PhysicsSteppingMethod property to adaptive.

.
Let me know what 'd you guys improve! :slight_smile:

-- Services
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local TweenService = game:GetService('TweenService')


local NPCTemp = ReplicatedStorage.NPC
local NPCContainer = workspace.NPC

local Settings = require(script.Settings)
local MapSize = 250
local MaxRoutes = 20

local Cubic = TweenInfo.new(0.25, Enum.EasingStyle.Linear)



NPC = {}
NPC.__index = NPC



function NPC:New()
	local self = setmetatable({}, NPC)
	
	local NewNPC = NPCTemp:Clone()
	NewNPC.Parent = NPCContainer
	
	self.NPC = NewNPC
	self.WalkSpeed = Settings.WalkSpeed
	self.Path = NPC:CreatePath()
	
	return self
end


function NPC:CreatePath()
	task.desynchronize()
	
	local Path = {}
	
	for I = 0, MaxRoutes do
		local X = math.random(-MapSize, MapSize)
		local Z = math.random(-MapSize, MapSize)
		
		local Pos = Vector3.new(X, 3, Z)
		local TravelTime = (I > 1 and (Pos - Path[I-1].Pos).Magnitude) or (Pos - Vector3.new(0,3,0)).Magnitude
		
		local Data = {
			TravelTime = TravelTime,
			Pos = Pos
		}
		
		table.insert(Path, Data)
	end
	
	task.synchronize()
	
	return Path
end


function NPC:Move()
	local Path: {} = self.Path
	local MaxPaths = #Path
	
	for I = 1, MaxPaths do
		
		
		local Pos: Vector3 = Path[I].Pos
		local TravelTime: number = Path[I].TravelTime
		
		
		
		local PrimaryPart: BasePart = self.NPC.PrimaryPart
		local WalkSpeed = self.WalkSpeed
		local PrimaryPos: Vector3 = PrimaryPart.Position
		
		
		local FlatTarget = Vector3.new(Pos.X, PrimaryPos.Y, Pos.Z)
		local LookAt = CFrame.lookAt(PrimaryPos, FlatTarget)
		local PosCF = CFrame.new(Pos) * (LookAt - PrimaryPos)
		
		
		local TweenStyle = TweenInfo.new(TravelTime / WalkSpeed, Enum.EasingStyle.Linear)
		
		
		local OrientationTween = TweenService:Create(PrimaryPart, Cubic, {CFrame = LookAt})
		local PosTween = TweenService:Create(PrimaryPart, TweenStyle, {CFrame = PosCF})
		
		OrientationTween:Play()
		OrientationTween.Completed:Wait(0.1)
		PosTween:Play()
		
		PosTween.Completed:Wait()
	end
end



return NPC

What’s the reasoning for using tween service as opposed to the humanoid MoveTo method? I’m going to assume it’s for performance reasons and you intend on simulating many NPCs without humanoids, but this could cause some issues to arise in certain situations, like collision detection with obstacles. The better method may be to use character controllers, but I’m not sure how performant that would be versus tweening in this situation. For certain uses, the tradeoff of using a character controller with its built in physics solving may be worth the performance drop in comparison to tween service. Of course this all depends on your use case.

Yeah i used TweenService instead of Humanoid:MoveTo() because of performance (and also the 8 second timer on Humanoid:MoveTo() )

i don’t plan my NPCs to be advanced, they’re merely just Visuals, that might be interactable by players & Their paths are already pre-made so i don’t need to do any expensive PathFinding calculations

the reason why i used math.random in the code above is because i was too lazy to make the paths lol

Im curious why you need to simulate hundreds of NPCs and whether theres a more efficient solution.

As to your question, it seems like youve already optimized as much as you can. I have a few suggestions.

1)since youre moving them along a fixed path, just anchor them.

  1. if all you do is move them then using Lerp() is faster than tween service.

3)id also turn touched and querry off of all the baseparts. And CastShadows

I Don’t really need hundreds of NPCs for my game, but if the game runs smoothly with hundreds, then it’ll run just fine with a couple,
i’ll try your suggestions!

Use skinned bone mesh character, it improve fps by a lot, but there is a catch, skinned mesh is inflexible (like ragdoll). And custom avatars took lots of time.
The most effective method is server calculates and client visualize but since you must need it to be in client so it not the right choice..

Bonus: instead of tween, Bulkmoveto is also good.