I would try something like this for your check:
if event.type==InputEvent.KEY and event.scancode == KEYUP and not event.echo:
if event.pressed:
getnode("SpatialPlayer/AnimationPlayer").play("running")
getnode("SpatialPlayer/AnimationPlayer").setspeed(0.5)
else:
getnode("SpatialPlayer/AnimationPlayer").play("playerstopwithanimgun")
getnode("SpatialPlayer/AnimationPlayer").setspeed(0.2)
The only code I added was "not event.echo." This will ignore continuous press events, which is actually what you want. Now, when KEYUP is pressed, the first "if" condition will happen. When KEYUP is let go, the second "if" condition will play.
As for why your program crashed, my guess is that your "if" statement runs the second condition for all events that aren't a KEYUP press. There are a lot of them. To see that, add this line to the beginning of your input function:
print(event)
You will see a ton of events written to the console. Each time, your second "if" condition happened.