Issue with GUI open and close

Hello, I am currently trying to fix my UI system, and make it so that when one UI is open, and a player clicks another button, it opens another UI while closing the previous one, so far I’ve gotten fairly close but there’s a few bugs that happen when I add tweaks.

My current code is

local Main = script.Parent
local Buttons = script.Parent.Buttons

local inventory = Buttons.inventory
local import = Buttons.import
local setting = Buttons.settings

local isOn = Main.isOn
local openFrame = nil

local function tween(i, t, p)
	local tweens = game:GetService("TweenService"):Create(i,t,p)
	tweens:Play()
	return tweens
end

for _,v in pairs(Buttons:GetChildren()) do
	if v:IsA("ImageButton") then
		v.MouseButton1Click:Connect(function()
			if openFrame and isOn.Value == false or isOn.Value == true then
				tween(openFrame, TweenInfo.new(1, Enum.EasingStyle.Back, Enum.EasingDirection.InOut), {Position = UDim2.new(-1, 0, 0.5, 0)})
				isOn.Value = false
				openFrame = nil
			end
			if isOn.Value == false then
				local gui = script.Parent[v.Name]
				tween(gui, TweenInfo.new(1, Enum.EasingStyle.Back, Enum.EasingDirection.InOut), {Position = UDim2.new(0.5, 0, 0.5, 0)})
				isOn.Value = true
				openFrame = gui
				print("on")
				print(isOn.Value)
			elseif isOn.Value == true then
				local gui = script.Parent[v.Name]
				tween(gui, TweenInfo.new(1, Enum.EasingStyle.Back, Enum.EasingDirection.InOut), {Position = UDim2.new(-1, 0, 0.5, 0)})
				isOn.Value = false
				print("off")
			end
		end)
	end
end

EDIT: script.Parent[v.Name] is just the frame that it opens.

My issue is, when I open the GUI using the button, it only transitions when I switch between different frames but if I click the same button in order to make it go back, it doesn’t fire the “if isOn.Value == true” meaning it never runs the part that prints off.

GIF of the issue

1 Like

nevermind, I found a solution for it.

Post it here in case someone has the same issue as you.
Common forum knowledge

1 Like