"It looks like you have the script on every single selectable/movable unit also trying to process issuing commands to all of those units. This adds inefficiency like having every active unit checking to see if it's the first in the list so it can command the others. I'd recommend separating those concerns. The scripts on each unit should handle only acting on commands, not issuing them to other units. Then you can have a single script instance elsewhere that handles issuing commands. I show an example of such an architecture in this answer" -DMgregory