Death mechaninic not working properly

I triede to make a system were after my character lost all of it’s HP, a death animation would play and once it was over the secene would change to a death screen, but when I try, I doesn’t work and the player never changes scenes

func _process(delta):
	if Input.is_action_pressed("Kill"):
		$AnimatedSprite2D.play("death")
		$Death_Timer.start()
	if global.hp < 0:
		$Life1.play("no life")
		$Life2.play("no life")
		$Life3.play("no life")
		$AnimatedSprite2D.play("death")
		$Death_Timer.start()
	$Label.text = ("SCORE: " + str(global.score))

func _on_death_timer_timeout():
	get_tree().change_scene_to_file("res://Scene/Tela_de_morte.tscn")

Once global.hp drops below zero, you call these lines on every frame. You should call them only once.

1 Like

How can I do it?

Just improve it a little bit. Define var death_timer_started = false at the top of the script, the change your code to:

if global.hp < 0 and not death_timer_started:
    // start your animations and the timer
    death_timer_started = true

It’s now working properly, but the animation doesn’t play

You might need to place some await lines between your animations.
ie:

	await get_tree().create_timer(3).timeout

Or you could use

await $AnimatedSprite2Danimation_finished

Though I would recommend checking if the signal exists, since I haven’t used animatedsprite2ds