My hero does nothing when enemy casts invisibility

In cavern survivals, my code functions flawlessly, but when encountering players casting invisibility, my hero freezes and becomes unresponsive. Despite implementing if-conditions, I’m unable to rectify this issue. It’s frustrating, as even low-tier players can gain a advantage by using invisibility, leading to unfavorable outcomes for me. Any guidance on resolving this problem would be greatly appreciated.

Code works completely fine with people not casting invis

And hero stands idle when players go invis.

Here’s a portion of my code, I even added a big if-else loop to check for enemyHero but still nothing works.

while True:
    distance = hero.distanceTo(enemyHero)
    headhunter = hero.findNearest(hero.findByType("headhunter"))
    brawler = hero.findNearest(hero.findByType("brawler"))
    if hero.canCast("sacrifice") and hero.gold >= hero.costOf("soldier"):
        hero.summon("soldier")
        soldiers = hero.findByType("soldier")
        soldiersInRange = [soldier for soldier in soldiers if hero.distanceTo(soldier) <= 30]
        nearestSoldier = hero.findNearest(soldiersInRange)
        if nearestSoldier:
            hero.cast("sacrifice", nearestSoldier, hero)
    if enemyHero:
        if hero.canCast("lightning-bolt"):
            hero.cast("lightning-bolt", enemyHero)
        if hero.isPathClear############ :
            if hero.pos.x > 70:
                targetPos = ############
            else:
                targetPos = ############
        else:
            if ############
                targetPos = ############
            else:
                targetPos = ############
        if hero.canCast("invisibility"):
            hero.cast("invisibility", hero)
        if hero.hasEffect("hide"):
            if ############:
                moveTo(targetPos)
        
        #elif headhunter and hero.distanceTo(headhunter) < 20:
            #hero.attack(headhunter)
        #elif brawler and hero.distanceTo(brawler) < 15:
            #hero.attack(brawler)
        
        elif enemyHero and hero.distanceTo(enemyHero) <= hero.attackRange:
            ############
            hero.attack(enemyHero)
            moveTo(targetPos)
        else:
            bestEnemy = findBestEnemy()
            if headhunter and hero.distanceTo(headhunter) < 20:
                if hero.isReady("devour"):
                    hero.devour(headhunter)
                hero.attack(headhunter)
            elif bestEnemy:
                hero.attack(bestEnemy)
    else:
        bestEnemy = findBestEnemy()
        if headhunter and hero.distanceTo(headhunter) < 20:
            if hero.isReady("devour"):
                hero.devour(headhunter)
            hero.attack(headhunter)
        elif bestEnemy:
            hero.attack(bestEnemy)
    if hero.health < hero.maxHealth * 0.8:
        if hero.canCast("regen"):
            hero.cast("regen", hero)
    ############