I made you an example of how it should work.
There is btw no need to set the values in global-variables
extends Control
onready var VolumeSound : HSlider = $VolumeSound
onready var VolumeMusic : HSlider = $VolumeMusic
# Set sliders when scene is loading
func _ready() -> void:
VolumeSound.value = get_linear_db("Sound") * VolumeSound.max_value
VolumeMusic.value = get_linear_db("Music") * VolumeMusic.max_value
# Some methods to make everything a bit nicer to read
func get_linear_db(bus_name : String) -> float:
assert(AudioServer.get_bus_index(bus_name) != -1, "Audiobus with the name " + bus_name + " does not exist.")
return db2linear(AudioServer.get_bus_volume_db(AudioServer.get_bus_index(bus_name)))
func set_linear_db(bus_name : String, linear_db : float) -> void:
assert(AudioServer.get_bus_index(bus_name) != -1, "Audiobus with the name " + bus_name + " does not exist.")
linear_db = clamp(linear_db, 0.0, 1.0)
AudioServer.set_bus_volume_db(AudioServer.get_bus_index(bus_name), linear2db(linear_db))
# Signals from sliders:
func _on_VolumeSound_value_changed(value: float) -> void:
set_linear_db("Sound", value / VolumeSound.max_value)
func _on_VolumeMusic_value_changed(value: float) -> void:
set_linear_db("Music", value / VolumeMusic.max_value)
Of course you have to adjust it to your scene.