I want to make the music stop playing when a player presses the mute music button.
I can’t figure out the mute part of the script.
I have tried looking at tutorials etc. but none helped with my issue.
This is my current code:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local AudioPlayer = require(ReplicatedStorage:WaitForChild("AudioPlayer"))
local startergui = game:GetService("StarterGui")
local mute = startergui.menu.mute
local unmute = startergui.menu.unmute
AudioPlayer.setupAudio({
["Elevator"] = 1845375096,
})
AudioPlayer.playAudio("Elevator")
function mutepress()
end
function unmutepress()
AudioPlayer.playAudio("Elevator")
end
mute.Activated:Connect(mutepress)
unmute.Activated:Connect(unmutepress)
One of your first issues is that you’re trying to access the buttons through StarterGUI. This won’t work as you will have to access the PlayerGUI in order to detect button activations.
Assuming that the music is going to be parented to SoundService, you may be able to run a simple loop for all the songs in there and mute them. I would keep the music player in a different script and have the mute and unmute button script in a local script in your ScreenGUI as so:
The localscript within the ScreenGUI is what actually has access to the mute and unmute. Try this and let me know if this works, also make sure its in the same place I have put it:
local MuteButton = script.Parent.Menu.Mute
local UnmuteButton = script.Parent.Menu.Unmute
MuteButton.Activated:Connect(function()
for i,v in pairs(game:GetService("SoundService"):GetChildren()) do
if v.ClassName == "Sound" then
v.Volume = 0 -- Mutes the volume
end
end
end)
UnmuteButton.Activated:Connect(function()
for i,v in pairs(game:GetService("SoundService"):GetChildren()) do
if v.ClassName == "Sound" then
v.Volume = 0.5 -- Change to desired volume
end
end
end)