I have tried to my best to try to fix this script. This is a mute script and I am trying to make it work. The mute part of the script works but when I try to press the text button again it doesn't do anything can you please help!
`m = workspace.Sound.Volume function Sound() workspace.Sound.Volume = 0 print'No music' script.Parent.TextLabel.Visible = true end script.Parent.MouseButton1Down:connect(Sound) function offSound() workspace.Sound.Volume = 0.8 print'music' script.Parent.TextLabel.Visible = false end script.Parent.MouseButton1Down:connect(Sound) `
Your TextButton is connect to the function Sound
, which from the looks of it only sets the volume of the sound object to 0. You are trying to have it toggled in correspondence with the click of the GuiObject the script in a child of.
Add a bool value that you will track, allowing you to execute a snippet of code when it's set to true, or false, depending on it's previous value.
local button, sound, toggle, bool do sound = workspace.Sound button = script.Parent bool = true -- set variables equal to corresponding values toggle = function () if sound then -- if sound and bool are existent bool = not bool --set bool to the opposite of it's current value -- run code based on true or false if bool == false then button.TextLabel.Visible = false print(button:GetFullName(), 'off') sound.Volume = 0; elseif bool then button.TextLabel.Visible = true print(button:GetFullName(), 'on') sound.Volume = 0.8; end end end -- connect the function to click of button (script.Parent) button.MouseButton1Click:connect(toggle) end
To toggle the volume you could just set the Volume to the opposite. If it is 0.8 set it to 0 else set it to 0.8.
The below could also be accomplished using an if statement.
script.Parent.MouseButton1Down:connect(function() workspace.Sound.Volume = (workspace.Sound.Volume == 0.8 and 0) or 0.8 script.Parent.TextLabel.Visible = not script.Parent.TextLabel.Visible end)