Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make a randomised music system with a mute button?

Asked by 2 years ago
Edited 2 years ago

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

0
Maybe create for I, v loop then create a variable and call the randomizer function. If a player wants to mute it create a image button GUI, attach a local script and make it so when it's clicked all of the sounds become disabled if that makes sense HeadZlear 0 — 2y
0
ty for replying, ive edited the question above to show what ive tried to do. the mute isnt working still but ive tried to annotate the script to my best ability. I only know how to code python so im kinda half guessing whenever i script - hope you can help, thanks :) User#47213 0 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

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 :)

0
You could also have just made it so there is a bool value in ur music script called Muted then made it so your music script contains the bool value as a variable and if it is false every song you have you put :Stop() after it stopping the music and not just muting it meaning if you were to unmute it would resume where it left off Katzencrafter21 0 — 2y
0
Luke good bro! How can I contact you? rikisamejima 2 — 2y
Ad

Answer this question