Well yes and no. It is sort of because you're using key_board_check_pressed, but also not really.
The condition is only true for one frame, which is probably what you want. However where ever you're setting sprite_index will be overriding the attack animation on the next frame.
There are a whole bunch of ways to approach this, but really what you want (I assume) is for the attack animation to play start-finish before reverting to what ever other animation should be playing (eg idle, walk/run, what ever).
Personally I (along with many other in the GMC) have a bit of an obsession with state machines so that is the approach I tend to take.
You could however declare a simple boolean variable like isAttacking = false to control when to play the animation, if you're not too familiar with statemachines. Like so:
Create Event
Step Event
Code:
mouse_left = mouse_check_button_pressed(mb_left);
//Attack
if (mouse_left) {
instance_create_layer(x+sign(hsp)*24,y,"Player",obj_hitbox);
image_speed = 1;
image_index = 0; //reset image index so that the attack animation will play from the start
isAttacking = true; //set the is attacking boolean to true
}
if(isAttacking == true){
sprite_index = spr_player_attack; //continually sets the sprite index to the attack sprite so long as boolean is true
}
Animation End Event
Code:
if(isAttacking == true){
isAttacking = false; //once animation is finished reset the attack boolean to false
}