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