Help with gui opening/closing

Screenshot 2021-02-23 at 092840
script which doesn’t show errors.

Screenshot 2021-02-23 at 092819
where the script is.

1 Like

Remove the if at the front of the connection, you only need open.MouseButton1Click:Connect(function().

You should also use a local script instead of a server script.

4 Likes

try switching to a local script
edit: also great panda is right remove the first if behind the function

2 Likes

You can just simply do:

local set = script.Parent
local open = set.Frame.verified_user
local opened = set.Main

open.MouseButton1Click:Connect(function()
    opened.Visible = not opened.Visible
end)

You can not use if statement for connections.
Additionally, you referred to the object in the StarterGui, not the PlayerGui. What this does is if your ScreenGui’s ResetOnSpawn property is checked, only then upon player reset, the changes will be shown.
Quick note: :connect() is deprecated, use :Connect() instead.

4 Likes

The elseif statements as we say should not be here. Replace it with else.

A few things that I see that could be causing it

  • The script should be a localscript in this case as it’s meant to be a local change to one player, as @TFlanigan said
  • There shouldn’t be an if infront of the open.MouseButton1Click event, it’s an event, not a function, as @GreatPanda3 mentioned
  • As @0V_ex managed to say before me, the code can be much more simplified by using opened.Visible = not opened.Visible. To explain what that does, it inverts the boolean (true/false), if say opened is not visible, this code will make it visible, and if it is visible, make it invisible. And typically yes Connect is used nowadays than connect

That should be all the things I see that need to be changed from what you have shown us which is why I didn’t really state my own thoughts.

My opinion on another thing you can do (Not an important change but if you want, open this)

Although in the future when you’re ever going to work with boolean checking, there’s something that, although not required, can be used when working with checking a single boolean

Say you have

local a = false

if a == false then
   --Code
end

Another way to write this is

```lua
local a = false

if not a then -- If a is false
   --Code
end

Like also shown, not can also act as an inverter for booleans. You can also do this for checking if a value is true

```lua
local a = true

if a then --If a is true
   --Code
else --If a was not true, then run this since if a wasn't true, then it's false. Anotehr thing that can be done when working with single boolean checks
  --Code
end
4 Likes

When a player joins, everything in StarterGui is cloned into a player’s PlayerGui, so you want to change the PlayerGui not the StarterGui.

Local Script inside of the ScreenGui

local player = game.Players.LocalPlayer
local set = player.PlayerGui.OpenB
local open = set.Frame.verified_user
local opened = set.Main

open.MouseButton1Click:Connect(function()
      opened.Visible = not opened.Visible
end)

Server scripts don’t run in StarterGUI (i think)

Scripts inside of UI objects work just like LocalScripts, as far as I’ve noticed. Correct me if I’m wrong here.
However, I prefer LocalScripts over Script just because UI objects are local.

  1. make it a local script
  2. Make the parent of the local script the button
  3. Use this script in the local script under the button
--local Instances
Local Gui = script.Parent.Parent.Parent

--Script
script.Parent.MouseButton1Click:connect(function()
    If Gui.Main.Visible == true then --Checks if gui is visible, and "closes it, if visible"/closes Frame
        Gui.Main.Visible = false
    elseif Gui.Main.Visible == false then --Checks if gui is visible, and "opens it, if invisible"/opens Frame 
        Gui.Main.Visible = true
    end
end)