try this
get_array_of_closest( org, array, excluders, max, maxdist ) { // pass an array of entities to this function and it will return them in the order of closest // to the origin you pass, you can also set max to limit how many ents get returned if( !IsDefined( max ) ) max = array.size; if( !isdefined( excluders ) ) excluders = []; maxdists2rd = undefined; if( isdefined( maxdist ) ) maxdists2rd = maxdist * maxdist; // return the array, reordered from closest to farthest dist = []; index = []; for( i = 0;i < array.size;i ++ ) { excluded = false; for( p = 0;p < excluders.size;p ++ ) { if( array[ i ] != excluders[ p ] ) continue; excluded = true; break; } if( excluded ) continue; length = distancesquared( org, array[ i ].origin ); if( isdefined( maxdists2rd ) && maxdists2rd < length ) continue; dist[ dist.size ] = length; index[ index.size ] = i; } for( ;; ) { change = false; for( i = 0;i < dist.size - 1;i ++ ) { if( dist[ i ] <= dist[ i + 1 ] ) continue; change = true; temp = dist[ i ]; dist[ i ] = dist[ i + 1 ]; dist[ i + 1 ] = temp; temp = index[ i ]; index[ i ] = index[ i + 1 ]; index[ i + 1 ] = temp; } if( !change ) break; } newArray = []; if( max > dist.size ) max = dist.size; for( i = 0;i < max;i ++ ) newArray[ i ] = array[ index[ i ] ]; return newArray; } check_zombie() { view_pos = self gettagorigin("tag_flash"); zombies = get_array_of_closest( view_pos, GetAiSpeciesArray( "axis", "all" ), undefined, undefined, 60 ); // 60 is the distance to see how far the zombie can be if ( !isDefined( zombies ) ) { return; } }