```
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 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)
```

2 Likes

He is right,

has to be IN the loop. Like:

```
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 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)
```

Greets

1 Like

Nice to hear.

You can ask everytime if you have another problem, the community is ready to help