Text was suposed to be changed but then be disable. but it just repeats

Godot Version

Replace this line with your Godot version

Question

this code below have the function that

if the player click at certain area2d it

just activates a panel alongside a rich

textlabel and if you clicked again it

woulve change but when i test the project the first text repeats twice before being deactivaded

instead of changing.

can someone tell me why this happens

		extends Area2D

var thoght = 0 
var entered = false


func _on_body_entered(body: Node2D) -> void:
	entered = true
	
	


func _on_body_exited(body: Node2D) -> void:
	entered = false
	

func _unhandled_input(event: InputEvent) -> void:
	if event.is_action_pressed("ui_change"):
		vthg()
		
		

func vthg():
	if thoght == 0 and entered == true:
		$"../CanvasLayer/Panel".set_deferred("visible", true)
		$"../CanvasLayer/Panel/HBoxContainer/VBoxContainer/RichTextLabel".set_deferred("text", "It's written 'Godym University'.")
		thoght = 1
	elif thoght == 1:
		$"../CanvasLayer/Panel/HBoxContainer/VBoxContainer/RichTextLabel".set_deferred("text", "Could you enter?")
		thoght = 2
	elif thoght == 2:
		$"../CanvasLayer/Panel".set_deferred("visible", true)
		$"../CanvasLayer/Panel/HBoxContainer/VBoxContainer/RichTextLabel".set_deferred("text", "It's written 'Godym University'.")
		thoght = 0

Is this script on more than one Area2D in the scene? The two elif branches don’t check entered, so another node running this same script can still advance its own thoght and write to that label on the same press, and I’m wondering if that’s where the first line is coming back from.

Something like this at the top would keep it to the area the player is actually standing in:

func vthg():
	if not entered:
		return

The other thing is that the thoght == 2 branch sets the same text as the thoght == 0 branch, and nothing in here ever sets visible back to false. If you were expecting the panel to close on that third press, it isn’t happening from this script.

thank you, its now working.