Issues Tweening GUI back and forth

Greetings,
I am attempting to tween my GUI back and forth depending on the click of a button (if button is pressed tween back, if back then tween forward.) Right now my code does nothing and I do not understand why.

Here is the code I have:

local StatsButton = script.Parent
local StatsFrame = script.Parent.StatsFrame.StatFrame
local OrangeFrame = script.Parent.StatsFrame.Orange
local LightOrangeFrame = script.Parent.StatsFrame.LightOrange

StatsButton.MouseButton1Up:Connect(function()
	
	 if StatsFrame.Position == UDim2.new(0.781,0,0.527,0)
	then 
		StatsFrame:TweenPosition(UDim2.new(1,0,0.527,0),"Out","Quint",1,false)
	elseif StatsFrame.Position == UDim2.new(1,0,0.527,0)	
	then
		StatsFrame:TweenPosition(UDim2.new(0.781,0,0.527,0),"In","Quint",1,true)	
	end
	
end)

Two things. First where is this script located and is it a local script?
Second, I probably wouldn’t use the position in comparison in case of floating point differences. Instead use a flag to say if its in one position or not to determine which way to go.

example:
local isForward = false

local isForward = false --Or true depending on which position it starts in.

StatsButton.MouseButton1Up:Connect(function()

	if isForward then
		--Move to backward position
		isForward = false
	else
		--Move to forward position
		isForward = true
	end

end)
1 Like

If you want more information on why you shouldn’t use Position to determine where the frame is with exact values, this is what it would look like if you printed a frames position:
{2.55200005, 0}, {0.319999993, 0}

Notice how it’s not exactly 2.5, 0, 0.31, 0? That’s floating point differences!

Emskipo has a great solution above and I would recommend going with that rather than using position.

change this:

StatsFrame:TweenPosition(UDim2.new(1,0,0.527,0),"Out","Quint",1,false)

to this:

StatsFrame:TweenPosition(UDim2.new(1,0,0.527,0),Enum.EasingStyle.Quint,Enum.EasingDirection.Out,1,false)

and change this:

StatsFrame:TweenPosition(UDim2.new(0.781,0,0.527,0),"Out","Quint",1,false)

to this:

StatsFrame:TweenPosition(UDim2.new(0.781,0,0.527,0),Enum.EasingStyle.Quint,Enum.EasingDirection.Out,1,false)
1 Like

sry i made a mistake in the post but i fixed it

local StatsButton = script.Parent.TextButton
local StatsFrame = script.Parent.StatFrame
local OrangeFrame = script.Parent.Orange
local LightOrangeFrame = script.Parent.LightOrange
local StatLabel = script.Parent.StatLabel

StatsButton.MouseButton1Up:Connect(function()
	
	 if StatsFrame.Position == UDim2.new(0.781,0,0.527,0)
	then 
		StatsFrame:TweenPosition(UDim2.new(1,0,0.527,0),"InOut","Quint",1,false)
	elseif StatsFrame.Position == UDim2.new(1,0,0.527,0)	
	then
		StatsFrame:TweenPosition(UDim2.new(0.781,0,0.527,0),"InOut","Quint",1,true)	
	end
	
	if OrangeFrame.Position == UDim2.new(0.781,0,0.485,0)
	then 
		OrangeFrame:TweenPosition(UDim2.new(1,0,0.485,0),"InOut","Quint",1,false)
	elseif OrangeFrame.Position == UDim2.new(1,0,0.485,0)	
	then
		OrangeFrame:TweenPosition(UDim2.new(0.781,0,0.485,0),"InOut","Quint",1,true)	
	end
	
	if LightOrangeFrame.Position == UDim2.new(0.781,0,0.484,0)
	then 
		LightOrangeFrame:TweenPosition(UDim2.new(1,0,0.484,0),"InOut","Quint",1,false)
	elseif LightOrangeFrame.Position == UDim2.new(1,0,0.484,0)	
	then
		LightOrangeFrame:TweenPosition(UDim2.new(0.781,0,0.484,0),"InOut","Quint",1,true)	
	end
	
	if StatLabel.Position == UDim2.new(0.789,0,0.497,0)
	then 
		StatLabel:TweenPosition(UDim2.new(1,0,0.497,0),"InOut","Quint",1,false)
	elseif StatLabel.Position == UDim2.new(1,0,0.497,0)	
	then
		StatLabel:TweenPosition(UDim2.new(0.789,0,0.497,0),"InOut","Quint",1,true)	
	end
	
end)

The issue was not the code but rather where everything was located, I just had to move some things around.

1 Like

I already have working code with the position system and am not having issues, the differentiation with floating values is so minute that I don’t think it matters. If it becomes an issue I’ll change other tweens over to your system. Thank you!

I see you repeat the same values over and over a bunch. I would recommend storing it as a variable. It is a pain to change it afterwards.

Remember the programming principle D.R.Y - Don’t Repeat Yourself.

No worries, just a suggestion of course :smiley:.
Glad its working now for you.

I would rather spend 10 minutes scripting an additional movement for an additional GUI then spending an hour putting the GUI into the same frame and moving it back to the position I had it in.

No need to spend that much time. I was talking about the positions. Saving them as a variable will help you in the long run.

General rule of thumb, if you use a value at least twice, save it.

Also, very interesting choice to leave “then” on the next line.

Glad to see that you solved it though. Best of luck with your project.

You probably want to flip the state variable before performing the tween.