UI Tweening Ignoring Close

Having problems with getting a UI tween to close again after I’ve opened the button.

When you click the button the first time it should open (which it does) and set the name to “Open” - which it has decided not to. Then once you click it again it should realise that the name is now “Open” and run the code that will close it.

Instead you get it opening fine, then when you go to close the UI it goes back to the opening animation.

local button = script.Parent

if button.Name == "Closed" then
local function onButtonActivated()
	button.AnchorPoint = Vector2.new(0, 1)
	button.Position = UDim2.new(0, 0, 1, 0)

	wait(1)
	button.AnchorPoint = Vector2.new(0.5, 0.5)
		button:TweenSizeAndPosition(UDim2.new(0.4, 0, 0.4, 0), UDim2.new(0.5, 0, 0.5, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quint)
		-- Perform expected button action(s) here
		wait(1)
		button.Name = "Open"
end
	button.Activated:Connect(onButtonActivated)
	wait(1)
else
		local function onButtonClick()
			print("Here")
			wait(1)
			button.AnchorPoint = Vector2.new(0, 1)
			button:TweenSizeAndPosition(UDim2.new(0, 200, 0, 50), UDim2.new(0, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quint)
			-- Perform expected button action(s) here
			wait(1)
			button.Name = "Closed"
		end
		button.Activated:Connect(onButtonClick)
end
local button = script.Parent

if button.Name == "Closed" then
	local function onButtonActivated()
		button.AnchorPoint = Vector2.new(0, 1)
		button.Position = UDim2.new(0, 0, 1, 0)

		wait(1)
		button.AnchorPoint = Vector2.new(0.5, 0.5)
		button:TweenSizeAndPosition(UDim2.new(0.4, 0, 0.4, 0), UDim2.new(0.5, 0, 0.5, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quint)
		-- Perform expected button action(s) here
		wait(1)
		button.Name = "Open"
		button.Text = "Open"
	end
	button.Activated:Connect(onButtonActivated)
	wait(1)
else
	local function onButtonClick()
		print("Here")
		wait(1)
		button.AnchorPoint = Vector2.new(0, 1)
		button:TweenSizeAndPosition(UDim2.new(0, 200, 0, 50), UDim2.new(0, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quint)
		-- Perform expected button action(s) here
		wait(1)
		button.Name = "Closed"
		button.Text = "Closed"
	end
	button.MouseButton1Click:Connect(onButtonClick)
end

You need to assign the “Text” property to change the text stored inside a TextButton instance, I’ve made that change.

local button = script.Parent
local toggle = false

button.MouseButton1Click:Connect(function()
	if button.Name == "Closed" then
		button.AnchorPoint = Vector2.new(0, 1)
		button.Position = UDim2.new(0, 0, 1, 0)
		task.wait(1)
		button.AnchorPoint = Vector2.new(0.5, 0.5)
		button:TweenSizeAndPosition(UDim2.new(0.4, 0, 0.4, 0), UDim2.new(0.5, 0, 0.5, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quint)
		-- Perform expected button action(s) here
		task.wait(1)
		button.Name = "Open"
		button.Text = "Open"
	elseif button.Name == "Open" then
		task.wait(1)
		button.AnchorPoint = Vector2.new(0, 1)
		button:TweenSizeAndPosition(UDim2.new(0, 200, 0, 50), UDim2.new(0, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quint)
		-- Perform expected button action(s) here
		task.wait(1)
		button.Name = "Closed"
		button.Text = "Closed"
	end
end)

Here’s the fix though.

1 Like
local button = script.Parent
local debounce = false

button.MouseButton1Click:Connect(function()
	if debounce then
		return
	end
	debounce = true
	if button.Name == "Closed" then
		button.AnchorPoint = Vector2.new(0, 1)
		button.Position = UDim2.new(0, 0, 1, 0)
		task.wait(1)
		button.AnchorPoint = Vector2.new(0.5, 0.5)
		button:TweenSizeAndPosition(UDim2.new(0.4, 0, 0.4, 0), UDim2.new(0.5, 0, 0.5, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quint)
		-- Perform expected button action(s) here
		task.wait(1)
		button.Name = "Open"
		button.Text = "Open"
	elseif button.Name == "Open" then
		task.wait(1)
		button.AnchorPoint = Vector2.new(0, 1)
		button:TweenSizeAndPosition(UDim2.new(0, 200, 0, 50), UDim2.new(0, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quint)
		-- Perform expected button action(s) here
		task.wait(1)
		button.Name = "Closed"
		button.Text = "Closed"
	end
	task.wait(3) --cooldown of 3 seconds
	debounce = false
end)

and with an added cooldown.

1 Like

Thanks for the help man! Appreciate it :raised_hands: