Im trying to work on unit movement for a game, it works if you make 1 unit, but after that it will lag a lot before moving,
local connections = {}
event.OnServerEvent:Connect(function(plr, vall, troopids, id)
for _, connection in pairs(connections) do
connection:Disconnect()
end
connections = {}
local units = workspace.Units:GetDescendants()
for _, unit in pairs(units) do
if unit.ClassName == "StringValue" and unit.Value == id and unit.Parent:WaitForChild("Ownership").Value == vall then
local cds = workspace.Nations:GetDescendants()
for _, cd in pairs(cds) do
if cd.ClassName == "ClickDetector" then
local connection = cd.RightMouseClick:Connect(function()
troopids.Parent:WaitForChild("Humanoid"):MoveTo(cd.Parent.Position)
end)
table.insert(connections, connection)
print(plr, troopids, id, vall)
end
end
end
end
end)