At the moment, I’m trying out a few ideas. I made a system that can successfully control 512 troops without any lag, the only problem now is collisions.
This got me to the conclusion, that I wanted them spaced out, like they do it in Mount & Blade: Bannerlord
When a unit finds a target, they go towards it and damage it. Simple. Problem is, when they start stacking up on top of each other, the frames drop faster than my will to live on a Monday.
Here is the code running the units, don’t worry about the framework bit, I use that to run the code instead of a while loop. I would like to note that this is just a temporary code that I made to test my idea. If I can get it to work, I will rework it to the best of my abilities.
Framework.NPC:AddNPC(nil,NPC,function()
if NPC and NPC.Parent and NPC:FindFirstChild("HumanoidRootPart") then
local ClosestEnemy = nil
for i,Enemy in pairs(CheckFolder:GetChildren()) do
local Distance = (HRP.Position - Enemy.HumanoidRootPart.Position).magnitude
if Enemy.Humanoid.Health > 0 and (not ClosestEnemy or Distance < ClosestEnemy[2]) then
ClosestEnemy = {Enemy,Distance}
end
end
if ClosestEnemy and ClosestEnemy[2] < 8 then
ClosestEnemy[1].Humanoid:TakeDamage(15)
else
local TooClose = nil
if TooClose then
elseif ClosestEnemy then
NPC.Humanoid:MoveTo(ClosestEnemy[1].HumanoidRootPart.Position - (CFrame.new(NPC.HumanoidRootPart.Position,ClosestEnemy[1].HumanoidRootPart.Position).LookVector * 6))
end
end
end
The solution code doesn’t have to be efficient for 256 troops (Which both sides have), but rather 6, as that’s how many a player would be able to control at once.