Tweened GUI comes in but how to make it go back?

So i’ve managed to tween a simple GUI from outside the screen , inside.
But now i am stuck on how to make the GUI go back if i click the same button again. I’ve searched on youtube and devforum and could not find anything (or maybe i didn’t knew the right terms of searching)

Anyway here is the script i did and also here is the placement in-game

Script :

local MenuButton = script.Parent.MenuButton.Button
local Menu = script.Parent.Menu
Menu.Position = UDim2.new(1.1,0,0.902,0) --Start Position

MenuButton.MouseButton1Click:Connect(function(OpenMenu)
	--Open Menu
	
	Menu:TweenPosition(
		UDim2.new(0.789,0,0.902,0), --End Position
		"InOut", --Easing Direction (where the tweeening will happen)
		"Quart", --Easing Style
		0.1,	 --Time in seconds
		false,   --Override other tweens
		nil,	 --Callback (function)
		false	 --Return
	)
end)

MenuButton.MouseButton1Click:Connect(function(CloseMenu)
	--Close Menu
	UDim2.new(-.789,0,0.902,0) --Start Position
	
	Menu:TweenPosition(
	UDim2.new(1.1,0,0.902,0), --End Position
	"InOut", --Easing Direction
	"Quart", --Easing Style
	0.1,	 --Time in seconds
	false,   --Override
	nil,	 --Callback
	false	 --Return
	)
end)

Placement :
image

This sounds easy to solve for most of the scripters here but keep in mind that i am new to scripting so please tell me if i did something wrong or if there are easier ways to solve this.

Thank you for helping.

1 Like

Instead of doing 2 different functions, make 1 and check if its opened or not.
For example instead of doing

MenuButton.MouseButton1Click:Connect(function(OpenMenu)
end)

MenuButton.MouseButton1Click:Connect(function(CloseMenu)
end)

Make something like this:

local Opened = false
MenuButton.MouseButton1Click:Connect(function()
       if Opened then
       --blablabla and set it to false
       else
       --blablabla and set it to true
       end
end)
1 Like
local db = false
MenuButton.MouseButton1Click:Connect(function()
    if db == false then
         Menu:TweenPosition(
		UDim2.new(0.789,0,0.902,0), --End Position
		"InOut", --Easing Direction (where the tweeening will happen)
		"Quart", --Easing Style
		0.1,	 --Time in seconds
		false,   --Override other tweens
		nil,	 --Callback (function)
		false	 --Return
	)
       db = true
    else
          Menu:TweenPosition(
	UDim2.new(1.1,0,0.902,0), --End Position
	"InOut", --Easing Direction
	"Quart", --Easing Style
	0.1,	 --Time in seconds
	false,   --Override
	nil,	 --Callback
	false	 --Return
	)
        db = false
   end
end)
2 Likes

You helped me in the other post i made a few days ago. Thank you so much <3