There are times when adding sleeps prevents lags. There are other times when adding them actually adds to lag; other times, like this, when it causes bugs. Just adding sleep in every loop without really understanding why you are doing it will cause a lot of problems. On the other hand, your "sleep 2" is vital and does what you want: prevents the script locking up.
As a rule of thumb, any script that would go on forever, or at least for an extended period, like while {true} or while { alive player } requires some sort of sleep in it or you will freeze the machine while it continually runs the script and doesn't take a break to update the game engine (graphics, physics, etc). If a script is finite (for "_i" from 1 to 10 do or with a forEach) then it probably shouldn't have a sleep, at least not to stop the game freezing. Obviously, there are lots of special cases, such as when the loop contains "slow" commands like createVehicle which, if you performed several times at once you would cause lag.
If in doubt, run without the sleep. If you get lag or freezing, add a sleep. Don't just put sleep in every loop for the sake of it.
OK, here is one of several scenarios that will break it: I am carrying ["RPO_obj3", "handgrenade"] and I pick up a "RPO_obj3" so I have ["RPO_obj3", "handgrenade", "RPO_obj3"],. The script comes around and looks at magazines select 0 and sees I have a "RPO_obj3". All well and good. It then waits 0.2 s, in which time I drop one of my first "RPO_obj3", which leaves me with ["handgrenade", "RPO_obj3"]. The script then checks magazines select 1, which is an "RPO_obj3" and adds that to the result. It then removes 2 obj3s and gives me an obj4, giving me: ["RPO_obj4"] and leaving a "RPO_obj3" on the ground that I pick up again. Net gain is one "RPO_obj3"!
Before you think, "but that would never happen!", believe me that it would do it occasionally and really confuse your players and you. More importantly a player could easily exploit it to generate sacks of money. In my example, the loop takes 2 + 0.2 + 0.2 = 2.4s. Therefore, if you repeatedly pick up and drop these objects in the right order you get a free object 0.2/2.4 = 1/12th of the time!
Well, removing that sleep will make the script work fine, but an overall better solution would be to use:
while {true} do
{
if (alive player) then
count _obj1 == 1) then
{
player removeMagazine "rpo_obj3";
player removeMagazine "rpo_obj3";
player addMagazine "rpo_obj4";
_obj1 = [];
};
Sleep 2;
};
EDIT: Pasted in wrong script...corrected in later post.