Tween GUI Position And Back

Hello there! I want to use the same button to change the GUI’s position in and out but I can’t find how to do this. This is my code so far, it only tweens it out.

local Leader = script.Parent.Parent.Leader
local Button = script.Parent

script.Parent.MouseButton1Click:Connect(function()
	Leader:TweenPosition(UDim2.new(2, 0, 0.04, 0),"Out","Bounce",4,true)
	wait(1)
	Button.Text = "<"
	wait(3)
	Leader.Visible = false
end)

use debounce for the button and when clicked while the leader is opened make the leader to tween back where it was

you can check out the debounce in and abou tweens in the dev hub

I’ve now tried to use debounce but when it goes out and I click on the button again, the only thing that changes if this ‘<’ to that ‘>’.

local Leader = script.Parent.Parent.Leader
local Button = script.Parent
local Debounce = false

script.Parent.MouseButton1Click:Connect(function()
	if not Debounce then
		Debounce = true
		Leader:TweenPosition(UDim2.new(2, 0, 0.04, 0),"Out","Bounce",4,true)
		wait(1)
		Button.Text = "<"
		wait(3)
		Leader.Visible = false
	elseif Debounce then
		Debounce = false
		Leader:TweenPosition(UDim2.new(1, 0, 0.04, 0),"In","Bounce",4,true)
		Leader.Visible = true
		wait(1)
		Button.Text = ">"
	end
end)

I think you could try something like this to simplify the code and also making it work

local Button = script.Parent
local Leader = button.Parent.Leader

local closePos = Button.Position
local openPos = UDim2.new(2, 0, 0.04, 0)

local opened = false

Button.MouseButton1Click:Connect(function()
	
	local posToUse = opened and closePos or openPos
	local direction = opened and "In" or "Out"
	
	Button.Text = opened and ">" or "<"
	Leader:TweenPosition(posToUse,direction,"Bounce",4,true)
	
	opened = not opened
end)

The tenary operator is good for condensing code to look simpler and organized instead of using the if statements, it basically works like this

local var = condition and valueIfConditionIsTrue or varIfConditionIsFalse

Let’s say you have

local var = x > y and x or y

This basically checks if x is greater tha ny, and if it true that x is greater than y, set var to x, otherwise, set it to y

Also I wasn’t sure if you wanted ot keep the visibility thing since I’m not sure if the close position is outside of the screen or not

Tell me if you have anymore issues!

1 Like