Script won't change a sound object's volume level

Hey.

I’m trying to get a script that changes the volume level to 0 or 1, depending on whether the player wants the volume muted or not. For some reason, the script does change the volume, but it still carries on playing. E.G, the volume is on 0, but I can still hear the audio.

This happens even when the sound:Play() and sound:Stop() has been executed so the sound starts fresh. I’ve tried every different combination of script as possible but nothing works. Any help would be greatly appreciated.

local toggleoff = script.Parent.ToggleOff
local toggleon = script.Parent.ToggleOn



toggleoff.MouseButton1Click:Connect(function()
	toggleoff.Visible = false
	toggleon.Visible = true
	game.StarterGui.DealershipScreen.Music.Volume = 0
end)

toggleon.MouseButton1Click:Connect(function()
	toggleon.Visible = false
	toggleoff.Visible = true
	game.StarterGui.DealershipScreen.Music.Volume = 1
end)

This script is in the toggle button shown in the video below:

1 Like

Hello! Make sure the script is a local script. Second, changing the volume’s sound in StarterGui does nothing to the player. The way StarterGui works is by replicating the descendants into a section inside the player called “PlayerGui.” Inorder to affect the player, you need to update the sound object in the PlayeGui. You can do this using this modified version of your script:

local player = game.Players.LocalPlayer -- gets the player
local playerGui = player.PlayerGui -- gets the player's player gui

local toggleoff = script.Parent.ToggleOff
local toggleon = script.Parent.ToggleOn

toggleoff.MouseButton1Click:Connect(function()
	toggleoff.Visible = false
	toggleon.Visible = true
	playerGui.DealershipScreen.Music.Volume = 0 -- changes the volume of the sound in the player gui
end)

toggleon.MouseButton1Click:Connect(function()
	toggleon.Visible = false
	toggleoff.Visible = true
	playerGui.DealershipScreen.Music.Volume = 1 -- changes the volume of the sound in the player gui
end)

For a better explanation on StarterGui, refer to this: StarterGui | Documentation - Roblox Creator Hub. However, note that the contents of a player’s PlayerGui are emptied upon death, so if a player dies, despite the music being toggled off, the next time the player enters the dealership, the sound may still keep playing.
If you have any questions let me know.

2 Likes

Hey. Thank you so much! It all works perfectly now. I completely forgot about PlayerGui :laughing:

2 Likes