Hi, I have a script right now that randomises the music on my game but the problem is I've tried but I just cant figure out how to add a mute button to it.
I don't mind changing the music system as long as it still picks a random song and can mute.
Script for random song:
wait(1) while true do local Sounds = script:GetChildren() local RandomIndex = math.random(1,#Sounds) local RandomSound = Sounds[RandomIndex] RandomSound:Play() wait(RandomSound.TimeLength) end
Script for mute gui:
local Music = workspace.Music:GetChildren() --grabs audio from underneath music script script.Parent.Value.Value = 0 -- sets mute gui value to 0 function onClicked() -- when mute button is pressed if script.Parent.Value.Value == 0 then for i,v in pairs(Music) do --for all the stuff below music script: if v:IsA("Sound") then -- if thing below music script is a Sound then v.Volume = 0 --set the volume to 0 script.Parent.BackgroundColor3 = Color3.fromRGB(202, 0, 0) --change mute gui colour to red script.Parent.Value.Value = 1 --sets mute gui value to 1 else v.Volume = 0.3 -- turns volume on script.Parent.BackgroundColor3 = Color3.fromRGB(29, 119, 214) --sets mute gui colour to normal script.Parent.Value.Value = 0 --sets mute gui value to 0 end end end end
I GOT THIS TO WORK
I'm going to explain for anybody who may look at this in the future :)
So basically i have a script and the children of the script are the Sounds which i want to use named "Sound1" "Sound2" and so on.
The script is this and its what's used for the random music to play and it's all you need for it to play. (Btw this one isn't my script i found it online)
wait(1) while true do local Sounds = script:GetChildren() local RandomIndex = math.random(1,#Sounds) local RandomSound = Sounds[RandomIndex] RandomSound:Play() wait(RandomSound.TimeLength) end
Then i have a MuteMusic GUI in Starter GUI which has an ImageButton and the children of that ImageButton is an Integer Value and a Local Script.
In that local script the code is:
local Music = workspace.Music:GetDescendants() function onClicked() if script.Parent.Value.Value == 0 then for i,v in pairs(Music) do if v:IsA("Sound") then v.Volume = 0 script.Parent.BackgroundColor3 = Color3.fromRGB(202, 0, 0) script.Parent.Value.Value = 1 end end else for i,v in pairs(Music) do if v:IsA("Sound") then v.Volume = 0.3 script.Parent.Value.Value = 0 script.Parent.BackgroundColor3 = Color3.fromRGB(29, 119, 214) script.Parent.Value.Value = 0 end end end end script.Parent.MouseButton1Click:Connect(onClicked)
You can change Getdescendants to GetChildren but might as well keep it as Descendants. Thanks to the guy that replied to my post, i watched a video on how to code i v in pairs and that's how i was able to do this.
It may be small for most of you but for me i completely scripted this mute button on my own and it's the first time that I've ever done that :)