How do I make this guy move back and forth?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By stealthyshiroean

Hello, I’m new to Godot and programming in general. I’ve been following this tutorial on Zenva and have put in the code exactly to make the enemy move back and forth. However, I keep on getting this error:

Invalid set index ‘x’ (on base: ‘Vector2’) with value of type ‘Nil’

This is the code that I wrote (pretty much copied but yeah…). If anyone can help me, please try to explain it in the most basic terms possible.

extends Area2D

export var speed : int = 100
export var moveDist : int = 100

onready var startX : int = position.x
onready var targetX : int = position.x + moveDist

func _process (delta):
	position.x = move_to(position.x, targetX, speed * delta)
	if position.x == targetX:
		if targetX == startX:
			targetX = position.x + moveDist
			targetX = startX

# moves "current" towards "to" at a rate of "step"
func move_to (current, to,step):
	var new = current

	# are we moving positive?
	if new < to:
		new += step	
		if new > to:
			new = to
		#are we moving negative?
			new -= step	
			if new < to:
				new = to			
		return new

Try moving last 5 lines of move_to one intendation to the left. Now the function doesn’t return anything if new >= to

A112Studio | 2020-05-27 15:51

Thank you so much! That’s exactly what it was.

stealthyshiroean | 2020-05-27 23:00