Tween UI Toggle

I’m trying to script a Toggle button in the UI Settings of my Game. I’ve successfully scripted it halfway through.

This is the current state of it:

The Red Button being pressed:
robloxapp-20220622-1725280.wmv (216.4 KB)

My issue is the fact that I can’t press it for it to be back to its previous state, including position and color. I normally am able to do it, but in this project I’m using Tween, something I have barely worked with before.

This is the current Script which works, as it breaks when I add “if - then”, “elseif - then”, “else”, or any other Conditional Structure:

local MusicSwitch = script.Parent.Parent.MusicSwitch

MusicSwitch.MouseButton1Click:Connect(function()
		wait(0.2)
		MusicSwitch:TweenPosition(
			UDim2.new(0.621, 0,-0.1, 0),
			"In",
			"Quad",
			.5,
			false
		)
		wait(0.4)
	MusicSwitch.BackgroundColor3 = Color3.fromRGB(108, 182, 73)
	MusicSwitch.BorderColor3 = Color3.fromRGB(108, 182, 73)
end)
2 Likes

Use a table for debounce, if you press the Music button, in the table[“Music”] = false or true, I think its better to do this instead table["Music"] = not table["Music"] to switch from true or false.

local MusicSwitch = script.Parent.Parent.MusicSwitch
local enabled = false

MusicSwitch.MouseButton1Click:Connect(function()
	if not enabled then
		task.wait(0.2)
		MusicSwitch:TweenPosition(UDim2.new(0.621, 0,-0.1, 0), Enum.EasingDirection.In, Enum.EasingStyle.Quad, 0.5, false)
		task.wait(0.4)
		MusicSwitch.BackgroundColor3 = Color3.fromRGB(108, 182, 73)
		MusicSwitch.BorderSizePixel = 0
		enabled = not enabled
	elseif enabled then
		task.wait(0.2)
		MusicSwitch:TweenPosition(UDim2.new(default position), Enum.EasingDirection.In, Enum.EasingStyle.Quad, 0.5, false)
		task.wait(0.4)
		MusicSwitch.BackgroundColor3 = Color3.fromRGB(default color)
		MusicSwitch.BorderSizePixel = 0
		enabled = not enabled
	end
end)
1 Like

Thank you so much! :happy3: This really helped a lot

1 Like

No problem! Just edited your code to be more efficient and readable.