Have a Area2D node that is a bullet

extends Area2D

var speed = 750
var target: RigidBody2D = null

func _ready():
	pass

func _physics_process(delta):
	if target and target.is_in_group("mobs"):
		var direction = (target.global_position - global_position).normalized()
		global_position += direction * speed * delta
	else:
		queue_free()

func set_target(new_target):
	target = new_target

func _on_area_entered(area):
	if area.is_in_group("mobs1"):
		print("hit")
		queue_free()

Player

func shoot():
	var b = bullet_scene.instantiate()
	owner.add_child(b)
	b.transform = $Marker2D.global_transform

	var enemies = get_tree().get_nodes_in_group("mobs")  # Replace with your enemy group name
	var closest_distance = 1000
	var closest_enemy = null

	for enemy in enemies:
		var distance = global_position.distance_to(enemy.global_position)
		if distance < closest_distance:
			closest_distance = distance
			closest_enemy = enemy

	if closest_enemy:
		b.target = closest_enemy

"But when the enemy calls queue_free(), it throws an error because of target = null. I want it to acquire a new target, and if the mob gets queue_free()'d before it reaches the target, I want it to continue moving in that direction."
how can i approach this 🙂

Please correct the code highlighting