How do I fix the attack animation so it plays the other animations after it has finished?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Pile_of_Ash

This is the Script I am using for my 2D character. The problem is when I attack the character plays the attack animation for a split second before cutting to another animation.

extends KinematicBody2D

var speed = 300
var max_speed = 75
var gravity = 450
var friction = 0.5
var jump = 200
var velocity = Vector2.ZERO
var resistance = 0.7
var jumpNumber = 1
var melee = false

var wallJump = 250
var jumpWall = 5

onready var sprite = $Sprite
onready var anim = $AnimationPlayer

func _ready():
pass # Replace with function body.

func _physics_process(delta):
var movement_x = Input.get_action_strength(“ui_right”) - Input.get_action_strength(“ui_left”)
if movement_x != 0:“Run”)
velocity.x += movement_x * speed * delta
velocity.x = clamp(velocity.x, -max_speed, max_speed)
sprite.flip_h = movement_x < 0
melee = false;
velocity.x = lerp(velocity.x, 0, friction)“Idle”)
melee = false

if Input.is_action_pressed("AttackHit"):"AttackSword")
	melee = true

if is_on_floor() or nextToWall():
	jumpNumber = 1
	if Input.is_action_just_pressed("ui_accept") and jumpNumber > 0:
		velocity.y -= jump
		jumpNumber -= 1"Jump")
		if $AnimationPlayer.current_animation == "Jump" and $AnimationPlayer.is_playing():
		if not is_on_floor() and nextToRightWall():
			velocity.x -= wallJump
			velocity.y -= jumpWall
		if not is_on_floor() and nextToLeftWall():
			velocity.x += wallJump
			velocity.y -= jumpWall
	if nextToWall() and velocity.y > 30:
		velocity.y = 30
		if nextToRightWall():
			sprite.flip_h = true"WallSliding")
		if nextToLeftWall():
			sprite.flip_h = false"WallSliding")
	if movement_x == 0:"Fall")
		velocity.x = lerp(velocity.x, 0, resistance)
		melee = false

velocity.y += gravity * delta
velocity = move_and_slide(velocity, Vector2.UP)

func nextToWall():
return nextToRightWall() or nextToLeftWall()

func nextToRightWall():
return $RightWall.is_colliding()

func nextToLeftWall():
return $LeftWall.is_colliding()

:bust_in_silhouette: Reply From: alexp

AnimationPlayer has a signal animation_finished( String anim_name), so connect a function to that to control melee, and put your other calls to play under
if not melee:

func _ready():
  anim.connect("animation_finished", self, "_on_animation_finished")

func _on_animation_finished(anim_name: String):
  if anim_name == "AttackSword":
    melee = false

Though you should probably look into managing your character with a state machine and use the current state to decide what animation should be playing at any given time.