The enemy does not change direction

I started using Godot 4.2.1 a few days ago, I was following a tutorial to program a patrolling enemy. Everything was working fine until I programmed it so that the enemy’s sprite would be inverted when he changed direction, but for some reason when the enemy hits the wall his sprite turns around but he continues walking forward and doesn’t change direction, getting stuck
I believe I have made something wrong in the programming, since the movement was working before, so I will leave the enemy code below. Whoever can help me, I would appreciate it.

extends CharacterBody2D

const SPEED = 900.0
const JUMP_VELOCITY = -400.0

@onready var wall_detector := $detector as RayCast2D
@onready var texture := $texture as Sprite2D

var direction := -1

Get the gravity from the project settings to be synced with RigidBody nodes.

var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)

func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta

if wall_detector.is_colliding(): 
	direction *= -1 
	wall_detector.scale.x *= -1
if direction == 1:
	texture.flip_h = true
	texture.flip_h = false

	velocity.x = direction * SPEED * delta


it could be your code here

triggered more than once, hence it looks like it never changed direction
try print(“something”) here to see if it’s accessed more than once, if that so, then you will need to add a boolean to check if it’s already changing direction