Ambience Mute Button Problem (I've found out how to fix it)

Okay, So I have this problem with one of my code (It works properly but doesn’t do how i wanted it to do)
So there’s this Ambience button that mutes the the sound but once you click “resume” and click “menu” and try to unmute it again you’ll have to double click which is annoying!

Menu

--//MENU//

MenuButton.MouseButton1Up:Connect(function()
	game.SoundService.Click:Play()
	game.SoundService.MenuMusic.Playing = true

	if MenuFrame.Position == UDim2.new(-0.5, 0, -0.1, 0) then
		Controls:Disable()
		MenuFrame:TweenPosition(UDim2.new(0, 0, -0.1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.25)
		MenuButton.Visible = false

		-- Increase Blur Size
		if Blur.Size <= 0 then
			repeat
				Blur.Size = math.min(Blur.Size + 5, 20)
				wait(0.125)
			until Blur.Size == 20
		end

		-- Adjust Ambience Volume
		if game.SoundService.Ambience.Volume <= 1 then
			repeat
				game.SoundService.Ambience.Volume = math.max(game.SoundService.Ambience.Volume - 0.5, 0.5)
				wait(0.125)
			until game.SoundService.Ambience.Volume == 0.5
		else
			game.SoundService.Ambience.Volume = 1
		end
	else
		-- Closing menu animation
		MenuFrame:TweenPosition(UDim2.new(-0.5, 0, -0.1, 0), Enum.EasingDirection.In, Enum.EasingStyle.Sine, 0.125)
		MenuButton.Visible = true
	end
end)

Resume

ResumeButton.MouseButton1Up:Connect(function()
	game.SoundService.CloseClick:Play()

	if MenuButton.Visible == true and MenuFrame.Position == UDim2.new(-0.5, 0, -0.1, 0) then
		-- Closing menu animation
		MenuButton.Visible = false
		MenuFrame:TweenPosition(UDim2.new(0, 0, -0.1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.25)

		-- Adjusting Ambience volume
		if game.SoundService.Ambience.Volume == 0.5 then
			repeat
				game.SoundService.Ambience.Volume = math.min(game.SoundService.Ambience.Volume + 0.5, 1)
				wait(0.125)
			until game.SoundService.Ambience.Volume == 1
		elseif game.SoundService.Ambience.Volume == 0 then
			game.SoundService.Ambience.Volume = 1
			repeat
				game.SoundService.Ambience.Volume = math.min(game.SoundService.Ambience.Volume + 0.5, 1)
				wait(0.125)
			until game.SoundService.Ambience.Volume == 1
		end
    else
		Controls:Enable()
		game.SoundService.MenuMusic.Playing = false
		MenuButton.Visible = true


		-- Closing other frames
		SettingsFrame:TweenPosition(UDim2.new(1.2, 0, -0.1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.25)
		UpdatelogFrame:TweenPosition(UDim2.new(1.2, 0, -0.1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.25)
		ProfileFrame:TweenPosition(UDim2.new(1.2, 0, -0.1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.25)
		MenuFrame:TweenPosition(UDim2.new(-0.5, 0, -0.1, 0), Enum.EasingDirection.In, Enum.EasingStyle.Sine, 0.125)

		-- Closing Blur effect
		if Blur.Size >= 1 then
			repeat
				Blur.Size = math.max(Blur.Size - 5, 0)
				wait(0.125)
			until Blur.Size == 0
		end

		-- Adjusting Ambience volume again
		if game.SoundService.Ambience.Volume == 0.5 then
			repeat
				game.SoundService.Ambience.Volume = math.min(game.SoundService.Ambience.Volume + 0.5, 1)
				wait(0.125)
			until game.SoundService.Ambience.Volume == 1
		elseif game.SoundService.Ambience.Volume == 0 then
			game.SoundService.Ambience.Volume = 1
			repeat
				game.SoundService.Ambience.Volume = math.min(game.SoundService.Ambience.Volume + 0.5, 1)
				wait(0.125)
			until game.SoundService.Ambience.Volume == 1
		end
	end
end) 

Ambience

if AmbienceMusicButton then
	AmbienceMusicButton.MouseButton1Up:Connect(function()
		if game.SoundService.Ambience.Volume == 0 then
			game.SoundService.Ambience.Volume = 1
			AmbienceMusicButton.TextColor3 = Color3.fromRGB(0, 255, 0)
		else
			game.SoundService.Ambience.Volume = 0
			AmbienceMusicButton.TextColor3 = Color3.fromRGB(255, 0, 0)
		end

		game.SoundService.Ambience.Playing = (game.SoundService.Ambience.Volume > 0)
	end)
else
	warn("AmbienceMusicButton not found. Make sure it is defined.")
end
1 Like

I’d recommend making a MenuIsOpen variable instead of checking the MenuFrame’s Position like this – this isn’t the cause of the issue, but it’s a code smell of its own.


Your issue is that the Ambience button is explicitly checking for Ambience.Volume == 0 to set the volume to 1 and otherwise sets it to 0. In your Menu script, you’re setting it to 0.5, which will fail the Ambience button’s check (causing it to set the volume to 0), and the 2nd click then sees that it is now 0, and sets it to 1.

I can’t judge what your intentions are with that “Adjust Ambience Volume” code, so I can’t suggest a fix.

1 Like

Thanks for trying to help me, I’m a new scripter so i don’t know the work arounds.

I’ll add a “MenuIsOpen” function!!!

A friend solve the problem, I added too many “==” which shouldn’t be on there