Some questions relating to GUI

Here is the script I have some questions about. (I’m a beginner)

  1. In the variable on the first line, why does :WaitForChild work but :FindFirstChild doesn’t?

  2. Starting on line 3, wouldn’t this also work too?

script.Parent.MouseButton1Click:Connect(function()
menu.Enabled = true
if menu.Enabled == true then
menu.Enabled = false
end
end)

(Relating to question 2, I understand the method currently being used is faster but I’m experimenting with code.)

Here is the scope of my explorer to assist with these questions.
image

1 Like

Menu spawns in AFTER the script runs, meaning that you need to use WaitForChild to wait for it to spawn in. FindFirstChild would work if the Menu spawned in before the script ran.

2 Likes

The menu spawns in after it runs? Isn’t it already in the explorer?

The scripts and UI replicate to the player’s PlayerGui when they join, and that is when the script runs.

For question 2, yes it is infact the same. I myself am allergic to the longer variants of that code, but I understand your concerns.

1 Like

I tried my variant as well but it didn’t work. Is there any error with it?

You never checked whether the menu was not visible before making it visible, meaning the if statement would always return true and it would always be invisible.

script.Parent.MouseButton1Click:Connect(function()
    if menu.Enabled == false then
        menu.Enabled = true
    elseif menu.Enabled == true then
        menu.Enabled = false
    end
end)
2 Likes

I have no clue what you’re talking about there :sweat_smile: it’s not a safety net, it’s just a quicker way of writing if statements when they are connected. You could also write it like this:

script.Parent.MouseButton1Click:Connect(function()
    if menu.Enabled == false then
        menu.Enabled = true
    else
        menu.Enabled = false
    end
end)