sound effect not played correctly

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By zombie

i tried to add a sound effect when the player dies but it only play when i enable autoplay and it will play in the beginning of the scene as well and that’s weird , help pls :slight_smile:
that’s the code :
extends Node2D

onready var death = $death
func _on_Area2D_body_entered(body):
if body.name==“player”:
get_tree().reload_current_scene()
death.play()
SOLVED

:bust_in_silhouette: Reply From: jgodfrey

Assuming you’re not getting any errors… I’d say that call to reload_current_scene() is not allowing any code following it (so, the call to play()) to be executed - since the scene is reloaded…

Comment out that line temporarily to see if it fixes the playing of the sound.

yess fixed but now the player is not dead what should i do ?

zombie | 2023-05-27 17:53

Ok, now that we’ve identified the problem, a simple fix would be something like:

onready var death = $death

func _on_Area2D_body_entered(body):
    if body.name == "player":
        death.play()
        yield(death, "finished")
        get_tree().reload_current_scene()

That should play the sound, wait for it to finish, and then reload the scene.

jgodfrey | 2023-05-27 18:17

thank you so much worked

zombie | 2023-05-27 18:20

1 Like