What Method should I use for Opening / Closing Gui?

Hi,
So I am wondering, which of the following should I use for an Opening / Closing script.
The following script is just to Hide the players UserId for a profile I’m making, I already know how to script it, but I’m just asking for support on which method I should use.

Method One: Two Scripts for Open and Close

Open Script:

script.Parent.MouseButton1Down:Connect(function()
script.Parent.Parent.PlayerUserId.Spoiler.Visible = false
script.Parent.Close.Enabled = true
script.Enabled = false
end)

Close Script:

script.Parent.MouseButton1Down:Connect(function()
script.Parent.Parent.PlayerUserId.Spoiler.Visible = true
script.Parent.Open.Enabled = true
script.Enabled = false
end)

Method 2: One Script Boolean

local Opened = false
script.Parent.MouseButton1Down:Connect(function()
	if Opened == false then
		Opened = true
	script.Parent.Parent.PlayerUserId.Spoiler.Visible = false
		
	elseif Opened == true then
		Opened = false
		script.Parent.Parent.PlayerUserId.Spoiler.Visible = true
		
	end
end)

I tested these out and they both seem to work the same. I’m just asking which one should I use.

Personally I like the second one. It’s just a bit more clear in my opinion.

Also you can actually shorten it a bit if you wanted.
script.Parent.MouseButton1Down:Connect(function()
	script.Parent.Parent.PlayerUserId.Spoiler.Visible = not script.Parent.Parent.PlayerUserId.Spoiler.Visible
end)

Though it might be a bit easier to read this way

local Opened = false
script.Parent.MouseButton1Down:Connect(function()
	Opened = not Opened
	script.Parent.Parent.PlayerUserId.Spoiler.Visible = Opened
end)

I deleted some of the code to simplify it, but yeah, you’re right

Well, I should also note that I only think the second one in the case you intend to use the same button. If you want different styles and locations to the button then I’d prefer the first. Though I’d keep it in the same script so all the similar functionality is in the same place if possible.

im pretty sure you can create a delay for it for stuff like TweenSize and TweenPosition.

I’m pretty sure you can because I’ve done it with a flashlight, however I’m just looking for opinions on what people think is the best

I think you could also use .Activated which also supports mobile.

Its better to use ButtonButton1Down because it only activates when you press your left mouse

If you don’t want to support mobile, use that. If you want to support mobile, use .Activated.

I dont plan on making any mobile supported games, maybe soon but not now

Solved but adding info for convenience:

.Activated passes an InputObject meaning you can customize what methods of input actually activate it.

Example: (Used @UMadara10’s code as a base)

local Spoiler = script.Parent.Parent.PlayerUserId.Spoiler

script.Parent.Activated:Connect(function(UserInput, ClickCount)
  -- This only allows Left click and touch to activate the button.
  if (UserInput.UserInputType ~= Enum.UserInputType.MouseButton1)
    and (UserInput.UserInputType ~= Enum.UserInputType.Touch) then return end
  Spoiler.Visible = not Spoiler.Visible
end)
1 Like

I rather just have a simple button, not really making a big project, either way, i just wanted opinions on 2 methods, not solutions

Also i dont think i need a UserInputService for Gui, just a simple click is all i need

@IDoLua @Maxi130TV @UMadara10 @tlr22
This is all I’m trying to do, Just a simple click:


Thank you for the solutions, but i wasn’t looking for solutions if you read it, i was just asking which was a good method for Opening / Closing UI

Since you all answered for Method 2, that will be the solution.

Thank you.

@IDoLua
My Mistake,
I thought .Activated, activated with all Mouse keys

For me, .MouseButton1Down activates on mobile devices. Not sure if I should be using .Activated.