Switch the two for-loops. Put the groups loop inside the player loop, then check if the player has the flags, and if he does, simply add "break" in the groups loop, so the other groups below it don't get checked. Also, don't get the player's name before making sure that he has the flags.