I need help with a issue with the full auto weapons in my fps game

godot 4

when i stop firing the full auto weapon, sometimes, seemingly randomly, it will instead of playing the idle animation, it will hold onto the last frame of the firing frame

heres the firing code (the rest of the code will be below) this problem i have is happening because i added await shit below if Input.is_action_just_released("main_fire_weapon") but i cant remove that, because it will cause the weapon to be able to be one tapped super fast:

func _process(delta):
	if Input.is_action_pressed("main_fire_weapon") and can_fire:
		Fire()
		await Weapon_Sprite.animation_finished
		can_fire = true
	if Input.is_action_just_released("main_fire_weapon"):
		await Weapon_Sprite.animation_finished
		can_fire = true
		Weapon_Sprite.play("Idle")

heres the node structure of the weapon:
Screenshot 2024-08-13 193419

full code:

extends Node3D

@onready var Weapon_Sprite = $CanvasLayer/Control/WeaponSprite

@onready var Hitscan = $Hitscan.get_children()

var can_fire = true

func _ready():
	Weapon_Sprite.play("Idle")
	
func check_hit():
	for ray in Hitscan:
		if ray.is_colliding():
			if ray.get_collider().is_in_group("Enemy"):
				ray.get_collider().take_damage(5)
	
func _process(delta):
	if Input.is_action_pressed("main_fire_weapon") and can_fire:
		Fire()
		await Weapon_Sprite.animation_finished
		can_fire = true
	if Input.is_action_just_released("main_fire_weapon"):
		await Weapon_Sprite.animation_finished
		can_fire = true
		Weapon_Sprite.play("Idle")

func Fire():
	check_hit()
	can_fire = false
	$Fire.play()
	Weapon_Sprite.play("Fire")

video:

There are multiple ways to correct this. As far as I can see, you can fire your weapon as long as the weapon_sprite animation is not playing

You can replace the can_fire variable with a can_fire() function. The function can look like this:

# Returns true of the animation is not playing and so the weapon can fire
func can_fire() -> bool:
     return not Weapon_Sprite.is_playing()

Then you can replace

if Input.is_action_pressed("main_fire_weapon") and can_fire:

with

if Input.is_action_pressed("main_fire_weapon") and can_fire():

You may need to do more alterations depending on your use case. At a later stage, you could add weapon cooldown or ammo checks to the can_fire function.

that didnt work out for me