Spinach Power Lua

Hi community!
I got stuck on the Spinach Power level and after scratching my head for a day I have no idea what’s wrong with the code:

local potCount = 0
local enemy = hero:findNearest(hero:findEnemies())
while true do
        local item = hero:findNearest(hero:findItems())
        if item then
            local potCount = potCount + 1
            if potCount == 7 and enemy then

The problem is my hero instead of getting the 7 potions, poke one of the ogres and then resumes the potion collecting. It quite leaves me speechless since it seems she doesn’t care about the “if potCount == 7 and enemy then” line.

Cheers, Cthonia

Your code first collect an item, then check the counter. As the result you attack once the enemy and your code start the whole cycle again.

You can break while-loop when counter >= 7 and start the new one with attack.
Or don’t collect potions if the counter >= 7.

Ah, I understand. Thanks for the reply!

ok so this is for 2 levels after spinach power but my code is right because when I sub out the “[1]
for another number it works.

// One gem is safe, the others are bombs.
// But you know the answer: always take the second.

while (true) {
    var items = hero.findItems();
    // If the length of items is greater or equal to 2:
    if(items.length >= 2){
        hero.moveXY(items[1].pos.x, items[1].pos.y);
        // Move to the second item in items
    // Else:
    else {
        hero.moveXY(40, 33);
        // Move to the center mark.

Hi there, please could you not post the same request for help in two places. It won’t get you help any quicker and clutters the forum.
:lion: :lion: :lion: