Help with my ui not closing with tween

I am making a system where I can open and close the ui. but the problem is that when you click on the ui it opens up and a tween plays you then can then click on other ui and the current ui or any other ui thats open will close with a tween. But when you click on the same ui the tween doesn’t play.

local Plr = game.Players.LocalPlayer
local TweenService = game:GetService("TweenService")

local OpenGUI = Plr.PlayerGui:WaitForChild("OpenGUI")
local ButtonGUI = script.Parent

local Switch = false
local GlobalSwitch = false

local TI = TweenInfo.new(
	0.2,
	Enum.EasingStyle.Quad,
	Enum.EasingDirection.InOut
)

function CloseOtherUI(SkipThisUI)
	for _, UI in pairs(OpenGUI:GetChildren()) do
		if UI == SkipThisUI then continue end
		
		if UI:IsA("Frame")  then
			local TweenClose = TweenService:Create(UI:FindFirstChildOfClass("UIScale"),TI,{Scale = 0})
			TweenClose:Play()
			TweenClose.Completed:Connect(function()
				UI.Visible = false
			end)
		end
		
	end
end


for _, button in pairs(ButtonGUI:WaitForChild("Buttons"):GetChildren()) do
	
	if button:IsA("TextButton") then
		button.MouseButton1Click:Connect(function()
			local UItoOpen = OpenGUI:FindFirstChild(button.Name)
			UItoOpen:FindFirstChildOfClass("UIScale").Scale = 0
			if UItoOpen then
				TweenService:Create(UItoOpen:FindFirstChildOfClass("UIScale"),TI,{["Scale"] = 1}):Play()
				
				if not GlobalSwitch then
					CloseOtherUI(UItoOpen)
					UItoOpen.Visible = not UItoOpen.Visible
					Switch =  not UItoOpen.Visible
					print(Switch)
				end
				
				
				end
			end)
	end
end

MB if the video laggy but hopefully you understand what I mean

1 Like
  • Consider caching referance to a tweens instead of creating a new one each time.
    You probably want to yield thread untill tween finishes: .Completed:Wait()
  • Consider moving non gameplay nesessary UI elements awey from healthbar as since it obscures vision and makes game look overly complex.
  • Dont use FindFirstChildOfClass/FindFirstChild if you are not even doing safety checks and instead index it dirrectly.
1 Like

These are good fixes to my code but they don’t help me with my task

You are not yielding the thread
It changes property to be visible instantly.

I see the point your tryinhg to make thanks

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.