Help optimizing this script? It uses 33% script activity with 1500 npcs, is there a best way to do it?
local partCache = require(game:GetService("ReplicatedStorage"):WaitForChild("PartCache"))
local bodyParts = {} -- npc model --> {head, arm, arm, leg, leg}
local animT = 0
local animR = 0
local animCf = CFrame.Angles(0,0,0)
local animCfInv = CFrame.Angles(0,0,0) -- * 1.57 == 180
game["Run Service"].PostSimulation:Connect(function(d) animT += d*40 animR = math.sin(animT) * .32 animCf = CFrame.Angles(animR,0,0) animCfInv = CFrame.Angles(-animR,0,0) end)
local caches = {
head = partCache.new(script.head,200,workspace.NPCS),
rightarm = partCache.new(script.rightarm,200,workspace.NPCS),
leftarm = partCache.new(script.leftarm,200,workspace.NPCS),
rightleg= partCache.new(script.rightleg,200,workspace.NPCS),
leftleg = partCache.new(script.leftleg,200,workspace.NPCS),
}
local function getBodyPart(bodyPartName:string)
return caches[bodyPartName]:GetPart()
end
local function newNPC(model)
local hum = model:WaitForChild("Humanoid")
local root = model:WaitForChild("HumanoidRootPart")
root.Transparency = 0
local head = getBodyPart("head")
local rightarm = getBodyPart("rightarm")
local leftarm = getBodyPart("leftarm")
local rightleg = getBodyPart("rightleg")
local leftleg = getBodyPart("leftleg")
head.Anchored = false
rightarm.Anchored = false
leftarm.Anchored = false
rightleg.Anchored = false
leftleg.Anchored = false
head.Weld.Part0 = root
rightarm.Weld.Part0 = root
leftarm.Weld.Part0 = root
rightleg.Weld.Part0 = root
leftleg.Weld.Part0 = root
if math.random(0,1)== 0 then
local connection = game["Run Service"].PreSimulation:Connect(function(delta)
--head.Weld.C0 = CFrame.new(0, 1, 0) * animCf
rightarm.Weld.C0 = CFrame.new(-1.5, 1, 0) * animCf
leftarm.Weld.C0 = CFrame.new(1.5, 1, 0) * animCfInv
rightleg.Weld.C0 = CFrame.new(-0.5, -1, 0) * animCf
leftleg.Weld.C0 = CFrame.new(0.5, -1, 0) * animCfInv
end)
bodyParts[model] = {head,rightarm,leftarm,rightleg,leftleg,connection}
else
local connection = game["Run Service"].PreSimulation:Connect(function(delta)
--head.Weld.C0 = CFrame.new(0, 1, 0) * animCf
rightarm.Weld.C0 = CFrame.new(-1.5, 1, 0) * animCfInv
leftarm.Weld.C0 = CFrame.new(1.5, 1, 0) * animCf
rightleg.Weld.C0 = CFrame.new(-0.5, -1, 0) * animCfInv
leftleg.Weld.C0 = CFrame.new(0.5, -1, 0) * animCf
end)
bodyParts[model] = {head,rightarm,leftarm,rightleg,leftleg,connection}
end
end
local function removedNPC(model)
if not bodyParts[model] then return end
for i = 1,#bodyParts[model] do
if typeof(bodyParts[model][i]) == "RBXScriptConnection" then
bodyParts[model][i]:Disconnect()
else
caches[bodyParts[model][i].Name]:ReturnPart(bodyParts[model][i])
end
end
bodyParts[model] = nil
end
workspace.NPCS.ChildAdded:Connect(function(npc)
if not npc:IsA("Model") then return end
newNPC(npc)
end)
workspace.NPCS.ChildRemoved:Connect(function(npc)
if not npc:IsA("Model") then return end
removedNPC(npc)
end)