Problem with founding the closest enemy

Godot Version

Godot_V4.3

Question

I have a code for founding a closest enemy, code checks which enemy does have higher progress, body in this case are PathFollow2d
This is the code:
func spot():
var enemy = $Area2D.get_overlapping_bodies()
if enemy.size() > 0:
for body in $Area2D.get_overlapping_bodies():
var telo = body.get_parent()
if telo.progress >= 0:
closest = telo

Problem with this code is that it gets the last body , or body with least progress. Thanks

seems like your code does not compare the progress of the stored closest variable to the next telo variable.

if telo.progress > closest.progress:

You can format code with three ticks ```

and where exactly should i put that line of code

I’m making a lot of assumptions about what closest is supposed to represent, I’m assuming closest is the telo with the highest progress. In this case you must only set closest = telo when the telo has a higher progress than closest, aka that if statement.