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

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:::

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.

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.

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.

Solved it, thank you…

Items, position …

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