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

How do I make this mute script work?

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

4 comments

0

Shouldn't line 18 be script.Parent.MouseButton1Down:connect(offSound)

2

are you trying to toggle on and off?

0

Yes i am trying to toggle it on and off

0

I did it was already on loop

2 answers

✓ Accepted answer

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

2 comments

0

@imagelabel this only turned in off...

0

I corrected the mistake, and reproduced it to ensure that it worked.

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)

4 comments

0

This only toggled the X I want to toggle the music as well

0

What?

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

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.

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