# How to make a array that sorts the enemies by closest to farthest?

I think you can use a for-loop to see the closest distance, and then use the append method to add them to a list

ill try to write this in code tmr and ill see if i can post it.

ok so i just had a look at the question again and im not sure that i can do that for now as im not that good. so far i can help you to find the furthest enemy or the nearest enemy. ill write out the code for both then ill post but i cant answer your question. srry

Heres the code to determine the nearest enemy to your hero:

``````enemies = hero.findEnemies()
distance = 9999
nearest = None
for enemy in enemies:
if hero.distanceTo(enemy) < distance:
distance = hero.distanceTo(enemy)
nearest = enemy
hero.say(nearest)
``````

or you can just use the function:

``````enemy = hero.findNearestEnemy()
hero.say(enemy)
``````

To find the furthest enemy use this code:

``````enemies = hero.findEnemies()
distance = 0
furthest = None
for enemy in enemies:
if hero.distanceTo(enemy) > distance:
distance = hero.distanceTo(enemy)
furthest = enemy
hero.say(furthest)
``````

hope it helps

Watery

Hello. I am a bit rusty, because I am coding on ROBLOX now.
But here is a function that may or may not work.

``````def closify():
enemies = hero.findEnemies()
nearest = 9999
nearestEnemy = null
for enemy in enemies:
if hero.distanceTo(enemy) < nearest:
nearest = hero.distanceTo(enemy)
nearestEnemy = enemy
``````

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.