Creating a button that destroys itself on being pressed with a button destroys both buttons!

Godot Version 4.7

Question

I wanted to make a button that spawns destroyable buttons when pressed and ran into the issue being that upon being pressed, the spawnable kills both itself and its parent. Help!

extends Button
func _ready():
	self.pressed.connect(_button_pressed)
func _button_pressed():
##	print("Until we meet again world!")s
##	get_tree().change_scene_to_file("res://menu.tscn")
	var button = Button.new()
	button.text = "KILL me"
	add_child(button)
	button.pressed.connect(_button_destroy)
func _button_destroy():
	self.queue_free()

Are you getting any errors?

self.Button2 doesn’t get child nodes, you would have to have a variable var Button2 in your script. The definition and how you set Button2 is going to be very important information as it’s the only thing .queue_free is being called on. Can you share your complete script?

the code block is wrong and the post was in pending so i couldnt edit it.
it is completely NOT what i intended to paste, edited it to resemble what it is currently

Thanks, self will always refer to the node this script is attached to, so self.queue_free() is deleting the initial button, including children but not specifically targeting any children.

You could bind the new child to this button destroy function

# etc...
	button.pressed.connect(_button_destroy.bind(button))

func _button_destroy(which_button: Button) -> void:
    which_button.queue_free()

or connecting to queue_free directly should work, but of course means you aren’t using your own function

button.pressed.connect(button.queue_free)

thank you now i can kill buttons to my hearts content