Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

1 comment

0

I'd like to point out that you do not keep the rep if your question is unanswered

3 answers

✓ Accepted answer

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

6 comments

0

you also forgot to mention that the children are potentially not all sound instances

0

This answer helped thanks and yes your not a stupid baby idiot, it turned off once :D

0

np

0

also uhhhm O_O O_O i feel incredibly nasty asking this but if the answer helped could you upvote it

0

sry I dont have enough rep

0

I gave you answer tho :D

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.

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

1 comment

0

This answer helped thanks you got the answer and its working good now. Every other answer had a good explanation too

This discussion is preserved as it was. New answers, comments, and votes are closed.