@hacknet yes, code outside the loop is executed every time your code is re-initialized globally. This is down to the way screeps code is running, and is called "global reset". You must work around that fact that it happens from time to time. Judging by the fact that you said it's happening every like 10 ticks, you're likely running it in sim. Simulation works a little bit differently from actual MMO server in that it resets globals a lot more often. On the actual MMO server due to the new IVM code your globals can often last for tens of thousands of ticks at once, but you still have to consider they will get reset once in a while.
If you absolutely want some of your code to only execute once and then never, you'll have to resort to using
Memory flags for a permanent trigger.