# Zero Sum: Collecting Coins

**URL:** <https://discourse.codecombat.com/t/zero-sum-collecting-coins/35984>\
**Category:** Level Help\
**Created:** [August 26, 2023, 9:39pm UTC](https://discourse.codecombat.com/t/zero-sum-collecting-coins/35984 "2023-08-26T21:39:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![milescombat](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milescombat/32/34822_2.png) [@milescombat](https://discourse.codecombat.com/u/milescombat)\
**Post date:** [August 26, 2023, 9:39pm UTC](https://discourse.codecombat.com/t/zero-sum-collecting-coins/35984/1 "2023-08-26T21:39:23Z")

</div>

My enemy Pender is collecting coins so fast. He just summons so many griffin-riders so quickly.  
PLS help me. I want to know how to collect coins fast too. Here is my code:

```python
while (true) {
    if (hero.gold >= 50) {
        hero.summon("griffin-rider");
    }
    let friends = hero.findFriends();
    for (let friend of friends) {
        let item = hero.findNearestItem();
        let enemy = friend.findNearestEnemy();
        if (enemy && friend && friend.type == "griffin-rider") {
            hero.command(friend, "attack", enemy);
        }
        if(hero.isReady("fear")) {
            hero.cast("fear", enemy);
        }
        if (item) {
            hero.move(item.pos);
        }
    }
}

```

PLS help!

---

<div class="post-metadata">

**Author:** ![JustALuke](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/justaluke/32/43608_2.png) [@JustALuke](https://discourse.codecombat.com/u/JustALuke)\
**Post date:** [August 26, 2023, 10:20pm UTC](https://discourse.codecombat.com/t/zero-sum-collecting-coins/35984/2 "2023-08-26T22:20:35Z")

</div>

Use the spell “goldstorm” to summon a cloud that spawns 105 gold coins over the course of about 9 seconds. I would also recommend focusing gold coins first to stay on the trail of the gold cloud.

---

<div class="post-metadata">

**Author:** ![milescombat](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milescombat/32/34822_2.png) [@milescombat](https://discourse.codecombat.com/u/milescombat)\
**Post date:** [August 27, 2023, 1:32am UTC](https://discourse.codecombat.com/t/zero-sum-collecting-coins/35984/3 "2023-08-27T01:32:06Z")

</div>

OK. I changed that, but she’s not trying to collect coins. She’s just trying to cast the “fear” spell.  
I also tried taking it away but she won’t move.

---

<div class="post-metadata">

**Author:** ![milescombat](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milescombat/32/34822_2.png) [@milescombat](https://discourse.codecombat.com/u/milescombat)\
**Post date:** [August 27, 2023, 1:32am UTC](https://discourse.codecombat.com/t/zero-sum-collecting-coins/35984/4 "2023-08-27T01:32:57Z")

</div>

By the way, I’m fighting you.

---

<div class="post-metadata">

**Author:** ![JustALuke](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/justaluke/32/43608_2.png) [@JustALuke](https://discourse.codecombat.com/u/JustALuke)\
**Post date:** [August 27, 2023, 10:46am UTC](https://discourse.codecombat.com/t/zero-sum-collecting-coins/35984/5 "2023-08-27T10:46:02Z")

</div>

You need to move your code for the hero (fear and item code) out of the for loop, otherwise it will only activate if you have friends.

---

<div class="post-metadata">

**Author:** ![PeterPalov](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/peterpalov/32/45280_2.png) [@PeterPalov](https://discourse.codecombat.com/u/PeterPalov)\
**Post date:** [August 27, 2023, 11:01am UTC](https://discourse.codecombat.com/t/zero-sum-collecting-coins/35984/6 "2023-08-27T11:01:52Z")

</div>

Yeah, also, if you’ve completed levels in Desert, you can write a BestItem function, with which you collect the best and nearest coins.

---

<div class="post-metadata">

**Author:** ![milescombat](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milescombat/32/34822_2.png) [@milescombat](https://discourse.codecombat.com/u/milescombat)\
**Post date:** [August 27, 2023, 5:32pm UTC](https://discourse.codecombat.com/t/zero-sum-collecting-coins/35984/7 "2023-08-27T17:32:15Z")

</div>

I did that. Pender is summoning two griffin-riders, but the second one doesn’t attack, and Pender isn’t collecting coins. I’ll show you my code one more time.

```python
while (true) {
    if (hero.gold >= 50) {
        hero.summon("griffin-rider");
    }
    let friends = hero.findFriends();
    for (let friend of friends) {
        let item = hero.findNearestItem();
        let enemy = friend.findNearestEnemy();
        hero.cast("goldstorm");
        if (item && item.value >= 2) {
            hero.move(item.pos);
        }
        if (enemy && friend && friend.type == "griffin-rider") {
            hero.command(friend, "attack", enemy);
        }
    }
}
if (hero.isReady("fear")) {
    hero.cast("fear", enemy);
}

```

---

<div class="post-metadata">

**Author:** ![milescombat](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milescombat/32/34822_2.png) [@milescombat](https://discourse.codecombat.com/u/milescombat)\
**Post date:** [August 27, 2023, 5:35pm UTC](https://discourse.codecombat.com/t/zero-sum-collecting-coins/35984/8 "2023-08-27T17:35:26Z")

</div>

I wish I could record my code.

---

<div class="post-metadata">

**Author:** ![JustALuke](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/justaluke/32/43608_2.png) [@JustALuke](https://discourse.codecombat.com/u/JustALuke)\
**Post date:** [August 27, 2023, 8:52pm UTC](https://discourse.codecombat.com/t/zero-sum-collecting-coins/35984/9 "2023-08-27T20:52:57Z")

</div>

`hero.cast("goldstorm");`  
You need to check if your hero is ready to cast goldstorm otherwise she will just sit there.

```python
if (item && item.value >= 2) {
    hero.move(item.pos);
}

```

If your hero sees an item that isn’t greater than or equal to 2 value she will just not do anything. You need to create a new function to determine which items are better value for how far they are from your hero, like in some of the desert levels.
