Invalid get index 'global_position' (on base: 'String')

Godot Version

V4.1.3

Question

i having this error pop up and idk how to fix can anyone help?
here my code `extends Area2D
var direction = Vector2.RIGHT
@export var speed = 250
var target = null
var state = 1

Called when the node enters the scene tree for the first time.

func _ready():
$return.monitorable = false
$return.monitoring = false
$Timer.start()

Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _physics_process(delta):
if state == 1:
rotate(0.4)
translate(direction.normalized() * speed * delta)
if state == 2:
rotate(0.4)
if target:
translate(direction * speed * delta)
direction = target.global_position - global_position
direction = direction.normalized()
func _on_timer_timeout():
state = 2
print(“hi”)
$return.monitoring = true
$return.monitorable = true

func _on_body_entered(body):
if body.is_in_group(“player”) && state == 2:
queue_free()

func _on_return_body_entered(body):
target = “player:<CharacterBody2D#32413582513>”
print(target)
print(“helo”)
`
here where the error pops up: direction = target.global_position - global_position

dont reference player like this

change

to

@onready target=

then drag and drop the Player Node from Scene Tree to the after =
it will auto generate path to it for you

but since you want it to get the player from body entered

then you can just do

target = body
1 Like