Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
2

My gui button isn't muting music when clicked. ?

Asked by 2 months ago

I have a gui TextButton that when its clicked it mutes all running music in the game, I have a folder in the gui that has the music. I used GetChildren() to get all musics to turn them off or set there volume to 0 but it shows an error in the output that says mute/unmute.LocalScript:14: attempt to index number with 'Volume' I cut the error in half because it had alot of refrencing so here it is in short to not waste your time :D

heres code, Thanks for helping

local button = script.Parent

local allMusic = button.Musics:GetChildren()

local unmuted = true
button.MouseButton1Up:Connect(function()
        if unmuted == true then         
            unmuted = false
            button.Text = "Music: OFF"
            for i,v in pairs(allMusic) do   -- beginning of error
                v.Volume = 0    -- The line that has problem.
            end         
        elseif unmuted == false then
            button.Text = "Music: ON"
            for i,v in pairs(allMusic) do
                allMusic.Volume = 1.3   -- Same problem here too.
                end
            unmuted = true      

    end

end)
0
I'd like to point out that you do not keep the rep if your question is unanswered WideSteal321 754 — 2mo

3 answers

Log in to vote
0
Answered by
Elyzzia 1229 Moderation Voter
2 months ago

if i'm not a stupid baby idiot, you were able to mute the music once before the error happened

that''s because when you did allMusic.Volume = 1.3, it added a new value to the allMusic table with the index "Volume" and the value 1.3

then, when you iterated over it, your script would try to change the volume of the number 1.3, since it's now a value in the table

to fix it you need to change allMusic.Volume = 1.3 to v.Volume = 1.3

also, i'd suggest renaming the unmute variable to mute, since it kind of describes the opposite of if the music is actually unmuted

0
you also forgot to mention that the children are potentially not all sound instances WideSteal321 754 — 2mo
0
This answer helped thanks and yes your not a stupid baby idiot, it turned off once :D ZombieApocalypz3 35 — 2mo
0
np Elyzzia 1229 — 2mo
0
also uhhhm O_O O_O i feel incredibly nasty asking this but if the answer helped could you upvote it Elyzzia 1229 — 2mo
View all comments (2 more)
0
sry I dont have enough rep ZombieApocalypz3 35 — 2mo
0
I gave you answer tho :D ZombieApocalypz3 35 — 2mo
Ad
Log in to vote
0
Answered by 2 months ago

Your problem was simple to fix all you had to do is for the For Loop you can't use the Table in this case the (allMusic) as allMusic.Volume = 1.3. For that line do v.Volume = 1

Also this will probably not work if the music aren't sound instances.

Log in to vote
0
Answered by 2 months ago
Edited 2 months ago

This is most likely due to the fact that the model (script.Parent.Musics) has some children that are not sound instances. A good way to fix this is to use :IsA(), this detects if an object is a type of object, e.g.:

local part = workspace.Part -- // part is the model (it is a part)

if part:IsA("Part") then -- // detect if it is a part
   part:Destroy(); -- //  code example
end

For you, you might want to try:

local button = script.Parent

local allMusic = button.Musics:GetChildren()

local unmuted = true
button.MouseButton1Up:Connect(function()
        if unmuted == true then         
            unmuted = false
            button.Text = "Music: OFF"
            for i,v in pairs(allMusic) do 
                if v:IsA("Sound") then
                  v.Volume = 0;
                end
            end         
        elseif unmuted == false then
            button.Text = "Music: ON"
            for i,v in pairs(allMusic) do
                if v:IsA("Sound") then
                v.Volume = 1.3; -- // also dont forget to set v's value not allMusics value
                end 
           end
         unmuted = true;
    end
end)

you do not need to use ; anywhere

0
This answer helped thanks you got the answer and its working good now. Every other answer had a good explanation too ZombieApocalypz3 35 — 2mo

Answer this question