I'm not sure if it will work for you, but I had the same problem and I just added a new variable called attacking, and then when you press the attack button, it sets attacking to true.
From there, there are a few lines of code below that check if you are attacking.
if Input.is_action_pressed("attack"):
attacking = true
if attacking:
#now play the attacking animation here
I'm not 100% sure that this will work, but I tried this out and it helped alot.
Edit: In case the player is moving whilst attacking, you can have 2 lines of code in each moving function:
if Input.is_action_pressed("right"): #can also work for left
if attacking:
attacking = true
else:
#play the run animation
I hope this works for you :)