Hello @Vollut and welcome to the forums! Feel free to reuse existing topics (related to this level) to ask your question next time.

What your code currently does is that it goes through the list of coins and calculates their distance. Once that is finished(!), it compares distance with nearestDistance. However, at this point distance holds the value of the distance of the last coin…

So what do you need to fix it?

You should check the distanceinside the while block, so you check it for every coin, not only the last one…

loop:
# define variables
while ...
# get coin distance
if distance < nearestDistance:
# set nearestCoin and nearestDistance
if nearest:
# move to coin

while True:
coins = hero.findItems()
coinIndex = 0
nearest = None
nearestDistance = 9999
# Loop through all the coins to find the nearest one.
while coinIndex < len(coins):
coin = coins[coinIndex]
coinIndex += 1
distance = hero.distanceTo(coin)
# If this coin's distance is less than the nearestDistance
if coin.distance < nearestDistance:
# Set nearest to coin
nearest = coin
# Set nearestDistance to distance
nearestDistance = distance
hero.moveXY(nearest.pos.x, nearest.pos.y)
# If there's a nearest coin, move to its position. You'll need moveXY so you don't cut corners and hit a trap
hero.moveXY(nearest.pos.x, nearest.pos.y)

First, the line after nearestDistance = distance shouldn’t be there. You’re telling your hero to move twice.

You’ve defined the variable, “nearest,” but you’re not checking to see if nearest: before trying to move there. You should always check to make sure that an object exists before acting on the object. In this case it should work because nearest always exists but don’t count on that. Just get in the habit of putting if statements first.

# This field is covered in firetraps. Thankfully we've sent a scout ahead to find a path. He left coins along the path so that if we always stick to the nearest coin, we'll avoid the traps.
# This canyon seems to interfere with your findNearest glasses!
# You'll need to find the nearest coins on your own.
while True:
coins = hero.findItems()
coinIndex = 0
nearest = None
nearestDistance = 9999
# Loop through all the coins to find the nearest one.
while coinIndex < len(coins):
coin = coins[coinIndex]
coinIndex += 1
distance = hero.distanceTo(coin)
# If this coin's distance is less than the nearestDistance
if coin.distance < nearestDistance:
# Set nearest to coin
nearest = coin
# Set nearestDistance to distance
nearestDistance = distance
# If there's a nearest coin, move to its position. You'll need moveXY so you don't cut corners and hit a trap.
if nearest:
hero.moveXY(nearest.pos.x, nearest.pos.y)

What’s coin.distance? Why don’t you use the variable you just defined on the line above.
When you set a variable like you did there: distance = hero.distanceTo(coin) It doesn’t become a property of coin, like coin.distance, instead it’s completely separate from the “coin”.
Danny

# Loop through all the coins to find the nearest one.
while coinIndex < len(coins):
coin = coins[coinIndex]
coinIndex += 1
distance = hero.distanceTo(coin)
# If this coin's distance is less than the nearestDistance
if coin.distance < nearestDistance:
coin.distance = nearestDistance
nearest = coin
# Set nearestDistance to distance
nearestDistance = distance
# If there's a nearest coin, move to its position. You'll need moveXY so you don't cut corners and hit a trap.
if nearest:
hero.moveXY(nearest.pos.x, nearest.pos.y)