[SOLVED]Mountain Mercenaries JavaScript help

// Gather coins to summon soldiers and have them attack the enemy.

while(true) {
    var coin = hero.findNearestItem();
    // Move to the nearest coin.
    // Use move instead of moveXY so you can command constantly.
    if (coin) {
        hero.move(coin.pos);
    }
    
    // If you have funds for a soldier, summon one.
    if (hero.gold > hero.costOf("soldier")) {
        hero.summon("soldier");
    }
    var enemy = hero.findNearest(hero.findEnemies());
    if (enemy) {
        
        var soldiers = hero.findFriends();
        var soldierIndex = 0;
        var soldier = soldiers[soldierIndex];
        // Loop over all your soldiers and order them to attack.
        while (soldierIndex < soldiers.length) {
            // Use the 'attack' command to make your soldiers attack.
            hero.command(soldier, "attack", enemy);
        }
    }
}

It says I have an infinite loop

Ah, I haven’t done level helps in a while, but I thought I’d give it a look. Welcome to the forum @Chanced!

1 Like

You need to increase soldierIndex every time the while loop runs. I had a similiar problem once :slight_smile:

Enjoy the Discourse!

1 Like

Thanks @Monsty22! How did you make 2 posts without six seconds passing?

np

Oh that happens to new users. It goes away once you go up a trust level.

1 Like

Oh, I see!

20 characters

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.