I’m currently making a tower defense game, and when i spawn in multiple enemies (100~ enemies) it lags the game, so i’m trying to achieve a more optimized spawning system, i currently have a simple system that uses pivotto and updating pos+ori values every frame.
i have this currently, i spawned over 500~ enemies
this is over 200~ enemies and counting
and i notice my recv is over 3 digits and the cpu ms being higher than usual, i want a finished product like this (https://media.discordapp.net/attachments/664903629546717218/933694377270845510/aw2_2.gif)
I am using attributes and values to update the enemy position and orientation every frame.
CLIENTSIDED SCRIPT
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local EnemyModels = ReplicatedStorage.Enemy
local EnemyAnims = ReplicatedStorage.Anims
local enemies = {}
local Enemies = workspace.Enemies
local onRenderStepped = function(dt)
for index, enemy in ipairs(enemies) do
if not enemy then
table.remove(enemies,index)
continue
end
local Value = enemy.Parent
local Position = Value:GetAttribute("EnemyPosition")
local Orientation = Value:GetAttribute("EnemyOrientation")
local PrimaryPart = enemy.PrimaryPart
PrimaryPart:PivotTo(CFrame.lookAt(Position, Orientation))
end
end
local function ChildAdded(child)
local Name = child.Name
local Model = EnemyModels[Name]:Clone()
Model.Parent = child
local Humanoid = Model.Humanoid
Humanoid:LoadAnimation(EnemyAnims[Name]):Play()
table.insert(enemies,Model)
end
RunService.RenderStepped:Connect(onRenderStepped)
Enemies.ChildAdded:Connect(ChildAdded)
SERVERSIDED SCRIPT
local runservice = game:GetService("RunService")
local rs = game:GetService("ReplicatedStorage")
local Vector3new = Vector3.new
local module = {}
local enemies = {}
runservice.Heartbeat:Connect(function() -- trying to make a heartbeat function that takes not much performance.
for i,v in ipairs(enemies) do
local speed = v:GetAttribute("Speed")
local PW = v:GetAttribute("Pathway")
local MovingTo = v:GetAttribute("MovingTo") -- these variables can be replaced with a table but instead im going for a created value within the enemymodels folder.
local map = workspace.map
local Pathway = map["Pathways"..PW]
if MovingTo > map:GetAttribute("MaxMovingTo") then
v:Destroy()
table.remove(enemies,i)
continue
end
if Pathway[MovingTo] then
local lastNode = nil
local currNode = Pathway[MovingTo]
if MovingTo - 1 == 0 then
lastNode = map["Start"..PW]
else
lastNode = Pathway[MovingTo - 1]
end
if currNode == nil then
if v ~= nil then
v:Destroy()
end
table.remove(enemies,i)
continue
end
local timecalc = tick() - v:GetAttribute("tick")
local magn = (currNode.Position - lastNode.Position).Magnitude
local dt = timecalc * speed/magn
v:SetAttribute("EnemyPosition", lastNode.Position:Lerp(currNode.Position, dt))
v:SetAttribute("EnemyOrientation", currNode.Position)
if dt >= 1 then
v:SetAttribute("MovingTo", MovingTo + 1)
v:SetAttribute("tick", tick())
end
else
v:Destroy()
table.remove(enemies,i)
end
end
end)
function module.CreateEnemy(Amount,Enemy)
for i = 1,Amount do
local Value = script[Enemy]:Clone()
Value.Parent = workspace.Enemies
Value:SetAttribute("tick", tick())
Value:SetAttribute("Pathways",1)
Value:SetAttribute("MovingTo",1)
table.insert(enemies,Value)
task.wait(0.25)
end
end
return module
please give me anything / any info that could increase performance greatly. I’d love seeing my recv be below 10 kb/s despite there being 600 enemies actively moving.