I thought you wanted that, enemy moving in infinte loop based on your comments.. How does something move to the left and right at the same time? If you want it to move left for n steps and right for n steps do this:
And if you don't want infinite loop, just set the state to "STOP" if you want to change the number of states just set the p.
var p = 0
var direction = "LEFT" # enum RIGHT/LEFT/STOP
func _physics_process(delta):
motion.y += GRAVITY
if direction == "LEFT":
motion.x = -SPEED
elif direction == "RIGHT":
motion.x = SPEED
p += 1
if p % 6 == 0:
# After 6 movements set a new direction
p = 0
if direction == "RIGHT":
direction = "LEFT"
if direction == "LEFT":
direction = "RIGHT
motion = move_and_slide(motion)