No bug. The expression loopindex() has only meaning in the scope of the event containing loop.
Once you go outside the loop, loopindex() has totaly no meaning no more.
The wait postpones the actions. So, those actions are executed outside the loop, they are even executed in some ticks after the loop ran.
Something close to this. You are moving (top - down) from the living room to the basement. You have the remote control of the TV in your hand. In the living room you must change channels for 10 times. No problem. Now comes the wait. Change channels 10 times over 5 minutes(*some time). By that time you are in the basement, outside reach from the TV. Out of scope.