Index p_bus = -1 is out of bounds

Godot 4.2.1

I’m trying to make a volume settings menu, but the sliders aren’t working. The game gives me these errors:

E 0:00:00:0675   audio_slider_settings.gd:22 @ setSliderValue(): Index p_bus = -1 is out of bounds (buses.size() = 3).
  <C++ Source>   servers/audio_server.cpp:924 @ get_bus_volume_db()
  <Stack Trace>  audio_slider_settings.gd:22 @ setSliderValue()
                 audio_slider_settings.gd:13 @ _ready()

E 0:00:00:0675   audio_slider_settings.gd:26 @ onValueChanged(): Index p_bus = -1 is out of bounds (buses.size() = 3).
  <C++ Source>   servers/audio_server.cpp:916 @ set_bus_volume_db()
  <Stack Trace>  audio_slider_settings.gd:26 @ onValueChanged()
                 audio_slider_settings.gd:22 @ setSliderValue()
                 audio_slider_settings.gd:13 @ _ready()

Here is my code:

extends Control

@onready var h_slider = $HBoxContainer/HSlider
@onready var label = $HBoxContainer/Label

@export_enum("Master", "Music", "Sfx") var busName: String

var busIndex: int = 0

func _ready():
	h_slider.value_changed.connect(onValueChanged)
	getBusNameByIndex()
	setSliderValue()
	
func setAudioNumText():
	label.text = str(h_slider.value * 100)

func getBusNameByIndex():
	busIndex = AudioServer.get_bus_index(busName)

func setSliderValue():
	h_slider.value = db_to_linear(AudioServer.get_bus_volume_db(busIndex))
	setAudioNumText()

func onValueChanged(value: float):
	AudioServer.set_bus_volume_db(busIndex, linear_to_db(value))
	setAudioNumText()

I’m sorry I’m not able to give a more detailed description of my problem, but I didn’t find anyone with a similar problem and I have no clue where to even start solving this issue. I re-watched the tutorial I was watching several times but couldn’t figure out what I did wrong.
Thanks to anyone who can help me figure this out. :slight_smile:

You probably have forgotten to provide a value to your export variable busName. Since you don’t provide a default value, it will be undefined when looking up the busIndex, thus get_bus_index (correctly) returns -1.

That was it, thanks :slight_smile: