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

How do I make this mute script work?

Asked by 9 years ago

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)
`
0
Shouldn't line 18 be script.Parent.MouseButton1Down:connect(offSound) UserOnly20Characters 890 — 9y
2
are you trying to toggle on and off? ImageLabel 1541 — 9y
0
Yes i am trying to toggle it on and off docrobloxman52 407 — 9y
0
I did it was already on loop docrobloxman52 407 — 9y

2 answers

Log in to vote
1
Answered by
ImageLabel 1541 Moderation Voter
9 years ago

Problem

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.

Solution

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
0
@imagelabel this only turned in off... docrobloxman52 407 — 9y
0
I corrected the mistake, and reproduced it to ensure that it worked. ImageLabel 1541 — 9y
Ad
Log in to vote
1
Answered by 9 years ago

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)
0
This only toggled the X I want to toggle the music as well docrobloxman52 407 — 9y
0
What? NotsoPenguin 705 — 9y
0
I'm not sure line 2 would work because the use of 'and' and 'or' return a bool value. (that's why they're so often used in if statements.) Perci1 4988 — 9y
0
@Perci1 The and and or operators do not always return bool values. The and operator returns the last argument or false. The or operator returns the first true argument or false. NotsoPenguin 705 — 9y

Answer this question