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! ![]()
-- 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