Coin sound not playing when collected, but it does play when it is spawned

Godot Version

V4.7

Question

Hi. So I encountered something very confusing.

I have a coin that can be collected by the player. When the player hits it, the coin counter goes up, but the sound doesn’t play.

I tested to see if a sound would play on the ready function and it does. It’s not quiet, and the range works. I will mention that the sound only seems to play on a spawned coin, but collecting that coin doesn’t play the sound.

Node setup:

image

Coin script:

extends Area2D

@onready var collect: AudioStreamPlayer2D = $Collect

var coin_speed : float = 150
var amount_to_give : int = 1
var coin_type : String = "Bronze"

func _ready() -> void:
	## This one plays the sound when the coin is spawned
	var sound_range : float = randf_range(0.9, 1.1)
	collect.pitch_scale = sound_range
	collect.play()

func _process(delta: float) -> void:
	# Goes across the screen
	position.x -= coin_speed * delta

func _on_body_entered(body: Node2D) -> void:
	if body is Player:
		## This does not play
		# Prints only once
		print("Test")
		var sound_range : float = randf_range(0.9, 1.1)
		collect.pitch_scale = sound_range
		collect.play()
		GlobalScore.coins += amount_to_give
		
	queue_free()

Example of how I spawn the coin using a timer with autostart. The timer is in a level:

func _on_coin_timer_timeout() -> void:
	var new_coin : Node = preload("res://Scenes/coin.tscn").instantiate()
	# Spawns slower coin that gives moe
	new_coin.coin_speed = 100
	new_coin.amount_to_give = 10
	$TempSpawner.add_child(new_coin)

You call queue_free() immediately after the sound starts playing. This deletes the audio player node and the sound stops playing practically before it even started.

Call queue_free() only after collect is done playing. You can use finished signal for that.

Okay, that’s working. Thank you.