I am a low level programmer and i can just suggest my approach.
GDScript dont support static variables so you need to make them global, if you need that a frame is only played once then you need to make an array(local to the module that is using it) parallel to each frame:
var playedframes[frames_num] = 
Fill it with zeros (int) and the beginning of your program,and when a frame is played you set it to 1,if is 1 then it's played and you dont' play it again:
playedframes[$AnimatedSprite.frame]=1 # set it to played
In your code there is a redundance by the way,this code is not needed:
elif $AnimatedSprite.frame != 0 and $AnimatedSprite.frame != 4:
stepPlayed = false
I suggest you to design your code structure better BEFORE writing lines to have an optimal result.