I have the following function, which, upon executing, is supposed to change the stream every time the rng gets a certain number

func Footsteps4(sample1, sample2, sample3, sample4): #Footsteps function for 4 samples
	print("called")
	var random_number = rng.randi_range(0, 4)
	match str(random_number): #Match the result of the rng as a string (text) - the result must be stored as a variable
		"0":
			if AudioPlayer.is_playing() == false: #So it doesn't changes stream mid playing
				AudioPlayer.stream = sample1
		"1":
			if AudioPlayer.is_playing() == false: #So it doesn't changes stream mid playing
				AudioPlayer.stream = sample2
		"2":
			if AudioPlayer.is_playing() == false: #So it doesn't changes stream mid playing
				AudioPlayer.stream = sample3
		"3":
			if AudioPlayer.is_playing() == false: #So it doesn't changes stream mid playing
				AudioPlayer.stream = sample4
			
	if AudioPlayer.is_playing() == false:
		AudioPlayer.play()

func _physics_process(delta):
	var obj = floor_raycast.get_collider()
	if floor_raycast.is_colliding() == true and $"../FloorRayCast".is_colliding() == true: 
		if Input.is_action_pressed("move_forward"):
			Footsteps4(PWalking1, Pwalking2, Pwalking3, Pwalking4)

The stream changes correctly, but the problem is that it changes once I release and press "move_forward", when I actually want it to change after the previous audio stream has finished.
What I want to do is change the stream every time one audio finishes, so the same stream isn't repeated all the time. What am I doing wrong?

  • Darxkl05 weird question - when you imported your sounds, did you make sure the Loop setting was off? Because many sound formats when imported default to that setting unless you change the import setting default in the project. Check the loop settings on your resource.

You probably want to do something where you cache the desired footstep sample and then use the Finished signal (docs) and change the AudioPlayer.stream there in the finished signal. That way, the stream will only change when the audio finishes playing.

You can then also remove if AudioPlayer.is_playing() == false: and:

if AudioPlayer.is_playing() == false:
		AudioPlayer.play()

and instead just have AudioPlayer.play() in the function attached to the Finished signal.

Yes it is what TwistedTwigleg said. In your source code, the random number generation can have 4 as the number. And your code only programmed up to 3.

Thanks for the answer, but unfortunately, the issue still persists. However, I think I figured out the issue.
Input.is_action_pressed("move_forward"): is called inside physics process, that means that if I press "move forward", the game will check it every frame. I actually intended for that to be there, as I want to check the ground every frame to change sounds. Putting this inside _input(event) caused some issues regarding the detection of new terrains

So, I think the issue is that the function is being executed every frame, which doesn't let the signal do its thing. The same sound just plays over and over again until I enter an area that enables the footsteps or when I press "move_forward" again.
I tried adding a boolean to execute the function only once, but it doesn't seem to work

var Sample1
var Sample2
var Sample3
var Sample4

onready var execute_function: bool

func _ready():
	floor_raycast.enabled = true

func Footsteps4(sample1, sample2, sample3, sample4): #Footsteps function for 4 samples
	if execute_function == true:
		var random_number = rng.randi_range(0, 3)
		match str(random_number): #Match the result of the rng as a string (text) - the result must be stored as a variable
			"0":
				if AudioPlayer.is_playing() == false: #So it doesn't change stream mid playing
					AudioPlayer.stream = sample1
					Sample1 = sample1 #Storing the currently used sound as a variable
			"1":
				if AudioPlayer.is_playing() == false: #So it doesn't change stream mid playing
					AudioPlayer.stream = sample2
					Sample2 = sample2
			"2":
				if AudioPlayer.is_playing() == false: #So it doesn't change stream mid playing
					AudioPlayer.stream = sample3
					Sample3 = sample3
			"3":
				if AudioPlayer.is_playing() == false: #So it doesn't change stream mid playing
					AudioPlayer.stream = sample4
					Sample4 = sample4
				
		if AudioPlayer.is_playing() == false: #This is to prevent the audios from playing every frame
			AudioPlayer.play()
		execute_function = false

func _physics_process(delta):
	var obj = floor_raycast.get_collider()
	if floor_raycast.is_colliding() == true: 
		if Input.is_action_pressed("move_forward"):
			execute_function = true
			Footsteps4(PWalking1, Pwalking2, Pwalking3, Pwalking4)
	elif floor_raycast.is_colliding() == false:
		AudioPlayer.stop()
							
	if Input.is_action_just_released("move_forward"):
		AudioPlayer.stop()

func _on_Footsteps_finished():
	Footsteps4(Sample1, Sample2, Sample3, Sample4) # Runs the function again with the same sounds so the random number changes`

Any help would be appreciated

    Darxkl05 weird question - when you imported your sounds, did you make sure the Loop setting was off? Because many sound formats when imported default to that setting unless you change the import setting default in the project. Check the loop settings on your resource.

      SnapCracklins Yep, that was it. Forgot to double-check something as simple as that. Thanks for the answer.
      I eventually removed the signal completely, and it still works.