Godot deselecting not working

godot 4.1

hello
extends Node2D

var selected = false
var mouse_offset
var draggable = true

func _process(delta):
if selected:
followMouse()
turn()

func followMouse():
position = get_global_mouse_position() + mouse_offset

func turn():
if Input.is_action_just_pressed(“left”):
rotation -= (PI / 2) / 2
if Input.is_action_just_pressed(“right”):
rotation += (PI / 2) / 2
if abs(rotation - 2 * PI) < 0.0001 or abs(rotation + 2 * PI) < 0.0001:
rotation = 0

func _on_area_2d_input_event(viewport, event, shape_idx):
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
if event.pressed:
mouse_offset = position - get_global_mouse_position()
selected = true
z_index = Global.test + 1
Global.test += 1
else:
selected = false

this is the code i’ve written for moving and rotating some puzzle pieces. i have a node2d with sprite2d and area2d
the thing is that when i rotate my object and the mouse cursor exits my shape, i can no longer deselct it. what should i do? thanks in advance

You are getting global mouse position but getting local position for this node.
Maybe get_local_mouse_position would work.

it doesn’t. the problem is that input doesn’t work. i tried the on mouse exited function, and then tried to get click input. but the input part doesn’t work.