A few optimizations.
_weapons = []; THEN agen - _weapons = (getWeaponCargo _object) select 1; Same with _magazines, _backpacks
_weapons THEN use only one time: count _weapons, same with _control,_magazines, _backpacks
use many time (typeOf _object), better use one variable for that: _type
FIX in loop:
one more check old this bag: https://www.youtube.com/watch?v=3ecWX21wEe4
Use this, if you think it's right