How to make this code do another line of code when button is clicked again

How to make this script do the next line of code when clicked again?

script.Parent.MouseButton1Click:Connect(function()
	game.Workspace.Scripts.GameScripts.BackgroundSound.Disabled = true
	game.Workspace.Scripts.GameScripts.BackgroundSound.Sound.Volume = 0
	script.Parent.Text = 'BACKGROUND SOUND OFF'
end)

I know how to reverse the code that does the stuff, but I don’t know how to make it do the opposite lines of code when clicked again

local toggle = false

script.Parent.MouseButton1Click:Connect(function()
	if not toggle then
		toggle = true
		game.Workspace.Scripts.GameScripts.BackgroundSound.Disabled = true
		game.Workspace.Scripts.GameScripts.BackgroundSound.Sound.Volume = 0
		script.Parent.Text = 'BACKGROUND SOUND OFF'
	elseif toggle then
		toggle = false
		game.Workspace.Scripts.GameScripts.BackgroundSound.Disabled = false
		game.Workspace.Scripts.GameScripts.BackgroundSound.Sound.Volume = 1
		script.Parent.Text = 'BACKGROUND SOUND ON'
	end
end)

I assume you’re trying to make it toggle between states, here you go.

simplified to:

local toggle = false
script.Parent.MouseButton1Click:Connect(function()
    toggle = not toggle
	game.Workspace.Scripts.GameScripts.BackgroundSound.Disabled = not toggle
	game.Workspace.Scripts.GameScripts.BackgroundSound.Sound.Volume = toggle and 1 or 0
	script.Parent.Text = "BACKGROUND SOUND "..(toggle and "ON" or "OFF")
end)
1 Like