First of all, you need to have separated animation sets for stand, walk with gun and without gun and also a separated animation set to pull the gun out.For example:
Animation 0: stand without gun
Animation 1: stand with gun
Animation 2: walk left with gun
Animation 3: walk left without gun
Animation 4: walk right with gun
Animation 5: walk right without gun
Animation 6: pull gun
If you have all the animation sets, there are several ways to do, what I would do, is to store the actual animation number (0,1,2,3,4,5,6) inside an object variable and change animation using the variable, like so:
always (or no condition): Do= player.Variable(animation) to current animation of player
Second, I would use a scene variable to store if the Z key is pressed. For example:
At the beginning of the scene and Z key is not down: Do =0 to Variable(zkeypressed)
If Z key is down and Variable(zkeypressed) = 0: Do =1 to Variable(zkeypressed)
If Z key is down and Variable(zkeypressed) = 1: Do =0 to Variable(zkeypressed)
This way I can use the zkeypressed variable to choose anytime if I want to use weapon or not, here is how:
To change animation, simply check the value of zkeypressed variable and change the value of animation variable
(I guess you are using A and D to move player left and right).
If Variable(zkeypressed) = 0 and key A is not down and key D is not down: Do = 0 to player.Variable(animation)
If Variable(zkeypressed) = 1 and key A is not down and key D is not down: Do = 1 to player.Variable(animation)
By default at the beginning if Z is not pressed, zkeypressed = 0 and because it 0, we change the value of object variable to 0.
Because Object variable is used to to choose animation, and because it value is 0, the stand without gun animation (Animation 0) is playing.
If you press Z, the zkeypressed variable is change to 1. If it 1 tha object variable is set to 1 which is to play stand with gun animation (Animation 1). If you press Z again, zkeypressed is change to 0 and because it 0, the object variable is change to 0 and play stand without gun animation. You can do the same for the rest of the animations.
Maybe sounds a bit complicated, but I hope it helps.
EDIT://
Oh yes, because in GD you can’t check if key is pressed only if key down, you have to make sure to events run only once if a key is down. Otherwise if you press a key (in this case Z) it runs multiple times and change value every frame as long the key is pressed and maybe the animation doesn’t change or you can’t see if it change because it happens super fast.
To make sure the event run only once every time you press a key, you can use a variable like so:
At the beginning of the scene and Z key is not down: Do =0 to Variable(zkeypressed)
If Z key is not pressed: Do = 0 to variable(pressed)
If Z key is down and Variable(zkeypressed) = 0 and Variable(pressed) = 0: Do =1 to Variable(zkeypressed) and Do = 1 to Variable(pressed)
If Z key is down and Variable(zkeypressed) = 1 and Variable(pressed) = 0: Do =0 to Variable(zkeypressed) and Do = 1 to Variable(pressed)