Play Home Contribute GitHub Join Us Discourse Staff Members GitHub Contribution Guides Team

Hoarding Gold help


Having trouble with “Hoarding Gold”. I’m getting an error on line 5 that says, "cannot read property ‘pos’ of null. What am I doing wrong? It doesn’t seem to me to be any different than any other time when my character has to read an X,Y position and move to it. Usually I use something like:

    var coin = findNearest(findItem());
    coinPosition = coin.pos;
    coinX = coin.x;
    coinY = coin.y;
    this.moveXY(coinX, coinY)

I tried using the above first but couldn’t make it work, so I tried toying with it to get what I’ve got now, and it still doesn’t work.

    var totalGold = 0;
loop {
    var coin = this.findNearest(this.findItems());
    if (totalGold < 25) {
    this.moveXY(coin.pos.x, coin.pos.y);
    totalGold = 0 + coin.value;
    } else if (totalGold >= 25) {
this.moveXY(58, 33);
this.say("Done collecting gold!");


cannot read property ‘pos’ of null.

Now, it’s not often explained clearly, but, you’re examining an item that doesn’t exist.

You are trying to check the ‘pos’ of something that doesn’t exist, specifically.

In this example, and in the future it’s important to check if your functions are actually returning something useful.

var coin = this.findNearest(this.findItems())
if(coin) {
  //Move to coin.
} else {
  //It didn't find any nearby coins!


Ah, I see now, I just needed to add something to test if there was a coin. I altered it to look for a coin AND look at the current value.

Appreciate your help, the problem was not very obvious to me.