# My hero does nothing when enemy casts invisibility

**URL:** https://discourse.codecombat.com/t/my-hero-does-nothing-when-enemy-casts-invisibility/36355
**Category:** Level Help
**Created:** [November 12, 2023, 1:50am UTC](https://discourse.codecombat.com/t/my-hero-does-nothing-when-enemy-casts-invisibility/36355 "2023-11-12T01:50:30Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Watamelon](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/watamelon/32/43741_2.png) [@Watamelon](https://discourse.codecombat.com/u/Watamelon)
#### Post date: [November 12, 2023, 1:50am UTC](https://discourse.codecombat.com/t/my-hero-does-nothing-when-enemy-casts-invisibility/36355/1 "2023-11-12T01:50:30Z")

</div>

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

 ![屏幕截图(41)](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/d/9/d94c01325d8abf9b612b6bc9efd1cb721f68cbaa.jpeg)

And hero stands idle when players go invis.

 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/4/7/47c4ec26776c40ce03fd0400ed6ca94af50e9563.jpeg)

---

<div class="post-metadata">

### Author: ![Watamelon](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/watamelon/32/43741_2.png) [@Watamelon](https://discourse.codecombat.com/u/Watamelon)
#### Post date: [November 12, 2023, 1:54am UTC](https://discourse.codecombat.com/t/my-hero-does-nothing-when-enemy-casts-invisibility/36355/2 "2023-11-12T01:54:36Z")

</div>

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

```python
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)
    ############

```
