TwinTails RenatoB : when you use "for each", you already pick each instance one by one, in a row. You don't have to filter more.
for each (local)
>> if local.occupied = 0
>>>> spawn a random sprite
>>>> set local.occupied = 1
I know isn't your current problem, but it's important to point out. Then, the logic of the code you provided looks correct otherwise. We need to know more about your code to solve this bug !
Cheers,
Nathan