Hey Sir LoLz,
Would something like this do what you're looking for?
flagSlotsRemaining = maxcarry - currentcarry
* Pick selected units.
* Loop - For each unit.
* flagSlotsRemaining >= 1. // If one or more slots are remaining.
* Call function "assignUnitTNextAvailableFlagSlot", Param 0 = unit.uid. //Or however you assign them.
* Deselect unit.
* recompute flagSlotsRemaining.
To make the above a little cleaner, you could break the "flagSlotsRemaining >= 1" condition into a sub-event, and place all the actions on that sub-event. And for a further, though likely negligible, optimization the sub-event could have a companion ELSE sub-event that breaks out of the loop if there are no more slots. That way, if there is only one slot left, and you select 100 units, you only run 1 loop iteration instead of 100 iterations.
You could also replace the "For each unit" loop with a "For each unit (ordered)" loop, and use the distance from the unit to the flag as the ordering formula. This would assign units to the flag, with priority going to the closest units first. (Closest may not always = shortest travel path in an RTS, but you might not want to compute a path length for all selected units just to start the loop.)