I’m trying to make it so that the player controls their army by clicking the mouse where the want the army to go, the army moves in formation.
The issue is that I keep getting a lag sometimes when the army is moving. The “lag” , Basically sometimes all of the npcs move, sometimes only some moves at different times than others.
This is what I am trying to accomplish all the time, with 100+ troops if possible https://gyazo.com/f2f643a2e63c70674766024c0b9e8bfb
This is how I move to troops.
local player = game:GetService("Players").LocalPlayer local Mouse = player:GetMouse() local activeArmy = workspace.Troops:GetChildren() local function MoveTo(pos) activeArmy = workspace.Troops:GetChildren() if #activeArmy < 1 then return end local xinc = 0 local yinc = 0 local inc = 2 local perRow = math.floor(math.sqrt(#activeArmy)) for i, v in pairs (activeArmy) do local position = pos + Vector3.new(xinc, 0, yinc) v.Humanoid:MoveTo(position) xinc += inc if xinc > (perRow * inc) then xinc = 0 yinc += inc end end end Mouse.Button1Down:Connect(function() local target = Mouse.Target if target and target.Parent then if target == workspace.SpawnLocation then return end local moveToCFrame = Mouse.Hit.Position MoveTo(moveToCFrame) end end)
I tried to make this as bare minimal as possible to see if I can figure out this issue, but I just can’t seem to find the solution. Am I just trying to do too much at once? I don’t know.
I don’t know if I’m just moving them ineffectively, or something else.
I’m not sure what else to try here. Any help is appreciated. If I have missed anything or didn’t explain something well enough, please feel free to ask and I’ll do my best to answer! Thank you!