Flip_h enemy logic not working

Godot Version

4.3

Question

`so basically im making an enemy script with 2 states (wander,chase) and when wandering the enemy would go to left bounds , change direction(andflip) and then go to right bounds and repeat until the player is in an area where the enemy can detect em but enemy sprite just doesnt work it looks right when moving left and vice versa here is my code :

@export var player : CharacterBody2D
@onready var sprite : AnimatedSprite2D = $AnimatedSprite2D
@onready var Detectionarea : Area2D = $detectionarea
var taking_damage_rn = false
var DIRECTION : Vector2
var right_bounds : Vector2
var left_bounds : Vector2
var Speed = 50
var ACCELERTATION = 300
var CHASE_SPEED = 100
var playerentered = false
@export var health : int = 150
var current_state = States.WANDER
enum States {WANDER,CHASE}
func _ready() -> void:
	left_bounds = self.position + Vector2(-20,0)
	right_bounds = self.position + Vector2(20,0)
func take_damage():
	health -= 10
	print(health)
func handle_movment(delta):
	if not taking_damage_rn:
		if current_state == States.WANDER:
			velocity = velocity.move_toward(DIRECTION * Speed, ACCELERTATION * delta)
			if not taking_damage_rn:
				$AnimatedSprite2D.play("walk")
		else:
			velocity = velocity.move_toward(DIRECTION * CHASE_SPEED , ACCELERTATION * delta)
			if not taking_damage_rn:
				$AnimatedSprite2D.play("attackrun")
	
	move_and_slide()
func change_direction():
	if current_state == States.WANDER:
		if sprite.flip_h:
			if self.position.x <= right_bounds.x:
				DIRECTION = Vector2(1,0)
			else:
				sprite.flip_h = false
		else:
			if self.position.x >= left_bounds.x:
				DIRECTION = Vector2(-1,0)
			else:
				sprite.flip_h = true
	else:
		DIRECTION = (player.position - self.position ).normalized()
		DIRECTION = sign(DIRECTION)
		if DIRECTION.x == 1:
			sprite.flip_h = true
		else :
			sprite.flip_h = false
func chase_player():
	$Timer2.stop()
	current_state = States.CHASE
func stop_chase():
	pass

func _process(delta: float) -> void:
	if sprite.flip_h == false:
		print("flip h = false")
	else:
		print("flip h = true")
		
	if health <= 0:
		$AnimatedSprite2D.play("new_animation")
		await(get_tree().create_timer(0.8).timeout)
		queue_free()
		
	handle_movment(delta)
	change_direction()
	look_for_player()

func look_for_player():
	if playerentered:
		chase_player()
	elif current_state == States.CHASE:
		stop_chase()
	elif current_state == States.CHASE:
		stop_chase()


func _on_player_youshouldtakedamagebadenemy() -> void:
	taking_damage_rn = true
	$AnimatedSprite2D.play("hurt")
	$AudioStreamPlayer2D.play()
	take_damage()
	print(health)


func _on_timer_timeout() -> void:
	$Timer.start()
	taking_damage_rn = false
func _on_detectionarea_body_entered(body) -> void:
	if body == player:
		playerentered = true

func _on_detectionarea_body_exited(body) -> void:
	if body == player:
		playerentered = false

You got the true/false switched up in the change_direction function. If your sprite is drawn pointing to the right as default, then flip_h will make it point to the left.
Fixing it should be as simple as putting swapping the contents of the if/else in the change_direction function, and setting the previously true to false and vice versa. See below for the new and fixed function.

New code for change direction function:

func change_direction():
	if current_state == States.WANDER:
		if sprite.flip_h: # The contents of this if/else have been swapped
			# If the sprite is flipped, that means we are going to the left
			if self.position.x >= left_bounds.x:
				DIRECTION = Vector2(-1,0)
			else:
				sprite.flip_h = false # I swapped this for it to work
		else:
			# If the sprite isn't flipped, we are going to the right
			if self.position.x <= right_bounds.x:
				DIRECTION = Vector2(1,0)
			else:
				sprite.flip_h = true # I swapped this too
			
	else:
		DIRECTION = (player.position - self.position ).normalized()
		DIRECTION = sign(DIRECTION)
		if DIRECTION.x == 1:
			sprite.flip_h = true
		else :
			sprite.flip_h = false

no way this actually worked tysm man I Might be dumb but also the chasing is behaving weird the enemy just walks infintely to the right instead of chasing the player?