You have to add the current direction to both if
. Maybe swap Vel.x <
and Vel.x >
between the ifs.
# Moving to the left
If Vel.x < 0 and globals.player.position.x < position.x
Vel.x = speed
Scale.x =-1
# Moving to the right
If Vel.x > 0 and globals.player.position.x > position.x
Vel.x = -speed
Scale.x = 1