# CODE, AUTO PICK UP ITEM (coins) (JS)

**URL:** <https://discourse.codecombat.com/t/code-auto-pick-up-item-coins-js/9056>\
**Category:** Uncategorized\
**Created:** [September 25, 2016, 1:42am UTC](https://discourse.codecombat.com/t/code-auto-pick-up-item-coins-js/9056 "2016-09-25T01:42:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![John31](https://avatars.discourse-cdn.com/v4/letter/j/67e7ee/32.png) [@John31](https://discourse.codecombat.com/u/John31)\
**Post date:** [September 25, 2016, 1:42am UTC](https://discourse.codecombat.com/t/code-auto-pick-up-item-coins-js/9056/1 "2016-09-25T01:42:45Z")

</div>

Hi There, Look I am sitting like 4 hours to know how to write a code that my hero could pick up coins automatically, So, Here is my code and I cant assign the position of Items:::

```python
while(true) {
    var enemies = hero.findEnemies();
    var enemy = hero.findNearest(enemies);
    var distance = hero.distanceTo(enemy);
    var items = hero.findItems();
    var coin = hero.findNearest(items);
    var x = coin.pos.x;
    var y = coin.pos.y;
    
    if (distance < 7) {
        hero.attack(enemy);
    }
    else {
        hero.moveXY(x, y);
    }
    
}

```

I don’t know it returns to change “coin.pos” to hero. pos, Non sense.

---

<div class="post-metadata">

**Author:** ![trotod](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/trotod/32/3263_2.png) [@trotod](https://discourse.codecombat.com/u/trotod)\
**Post date:** [September 25, 2016, 4:03am UTC](https://discourse.codecombat.com/t/code-auto-pick-up-item-coins-js/9056/2 "2016-09-25T04:03:43Z")

</div>

You’ll need to check if `coin` is `null`, as if there are zero `items`, then there is no nearest coin. If something is null, then it has no properties to access, and throws an error.

Though the error message could definitely be made better.

---

<div class="post-metadata">

**Author:** ![John31](https://avatars.discourse-cdn.com/v4/letter/j/67e7ee/32.png) [@John31](https://discourse.codecombat.com/u/John31)\
**Post date:** [September 25, 2016, 6:52am UTC](https://discourse.codecombat.com/t/code-auto-pick-up-item-coins-js/9056/3 "2016-09-25T06:52:20Z")

</div>

Thank you, but there is all around me a lot of coins. And findItem I hope finds coins? But It makes that array, so I give another function findNearestItem and it should give just one coin. Am I right? Hopefully yes. So. I can’t figure out why it still throws an error? Or Am I doing positioning incorrect?  
I mean how to assign position coordinates of an item.

item.pos.x = somethin.  
item.pos.y = something else. Yes? Or some different?

Its Multiplayer treasure grove.

---

<div class="post-metadata">

**Author:** ![John31](https://avatars.discourse-cdn.com/v4/letter/j/67e7ee/32.png) [@John31](https://discourse.codecombat.com/u/John31)\
**Post date:** [September 25, 2016, 7:47am UTC](https://discourse.codecombat.com/t/code-auto-pick-up-item-coins-js/9056/4 "2016-09-25T07:47:37Z")

</div>

Solved it, thank you…

---

<div class="post-metadata">

**Author:** ![John31](https://avatars.discourse-cdn.com/v4/letter/j/67e7ee/32.png) [@John31](https://discourse.codecombat.com/u/John31)\
**Post date:** [September 26, 2016, 7:11am UTC](https://discourse.codecombat.com/t/code-auto-pick-up-item-coins-js/9056/5 "2016-09-26T07:11:59Z")

</div>

Items, position …

itemPos = item.pos  
itemX = itemPos.x  
itemY = itemPos.y
