Guards won't move I'm probably stupid [javascript]

My issue has to do with the guardAct() function. I made an array to find the nearest monsters, but and classified it so that my guards would not cross onto the other side, but my guards won’t move at all. Please help.

There is also an error when the ai tries to read the x-pos of the enemy.

function guardAct(data) {
    const guard = data.guard;
    const place =;
    while (true) {
        var enemyIndex = 0;
        var enemies = guard.findMonsters();
        while(enemyIndex < enemies.length){
            var enemy = enemies[enemyIndex];
            if (enemy) {
                if(enemy.pos.x < 55){
                    }else if(guard.isReady('haste')){
                    enemyIndex +=1;

hero.on('spawn-guard', guardAct);
function begin(){
    var archerBack ='archer', "A");
    hero.setTargeting(archerBack, 'strong');'cannon', 'B');'cannon', 'C');
    hero.special('push', 13, 51);'archer', 'H');'archer', 'I');
    hero.special('teleport', 21, 51);'archer', "F");'archer', "G");'mage', 'E');'mage', 'D');
    hero.special('pull', 40, 51);'archer', 'A');
    hero.special('teleport', 38, 51);

Monsters don’t have pos property as the first, also with such code you easily can get an infinite loop as if an enemy is dead (easily can be as beween cycles can be several seconds, then you get repeating first if. be careful with while loops and check that you always increment a counter or you have break condition.