Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

There's a problem with my open/close gui?

Asked by 4 years ago

Hey I'm trying to make a open/close button but I ran into an hurdle that I couldn't really overcome. If possible, can you explain why my code isn't working as intended?

Here's the code:


local function open() local menu = game.StarterGui.ScreenGui.menu local frame = game.StarterGui.ScreenGui.Frame frame.Visible = true end local function close() local menu = game.StarterGui.ScreenGui.menu local frame = game.StarterGui.ScreenGui.Frame if frame.Visible == true then frame.Visible = false end end local menu = game.StarterGui.ScreenGui.menu menu.MouseButton1Down:Connect(open()) menu.MouseButton1Down:Connect(close())

Thanks!

0
make sure that the code is in local script and also 'menu' is a screengui or a button? kujekmarek 24 — 4y

3 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

First of all, you're making changes to the Frame that's inside the StarterGui, which is wrong, once a player joins the game everything inside the StarterGui will move into their PlayerGui. So you'll need to get the GUI from their PlayerGui and make changes to that for them to see those changes.

You could just do this which is a much simpler method.

local Player = game.Players.LocalPlayer
-- You'll most likely need to change these variables to where your GUIS are located inside the Player.
local ScreenGui = Player.PlayerGui.ScreenGui
local Menu = ScreenGui.menu
local Frame = ScreenGui.Frame

Menu.MouseButton1Down:Connect(function()
    Frame.Visible = (not Frame.Visible)
end)
0
Hey could you explain the 'not', I understood everything apart from that part. Thanks! AtrocityPrevails 49 — 4y
2
All the not function does is it does the oposite if something for e.g. If it was true then it will be false JesseSong 3916 — 4y
Ad
Log in to vote
1
Answered by 4 years ago

Try this

local function open()
    local menu = game.StarterGui.ScreenGui.menu
    local frame = game.StarterGui.ScreenGui.Frame
    frame.Visible = true        
end

local function close()
    local menu = game.StarterGui.ScreenGui.menu
    local frame = game.StarterGui.ScreenGui.Frame
    frame.Visible = false
end

local menu = game.StarterGui.ScreenGui.menu
local frame = game.StarterGui.ScreenGui.Frame

menu.MouseButton1Down:Connect(function()
    if frame.Visible == true then
        close()
    else
        open()
    end
end)

I haven't tested it out, so hopefully it works!

Don't forget to upvote and make this solution IF this works :)

0
Wrong use playergui rather than startergui and use a not JesseSong 3916 — 4y
0
Hey that didn't really work but now I see the problem, thanks for trying to help anyway! :) AtrocityPrevails 49 — 4y
Log in to vote
1
Answered by
JesseSong 3916 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

The two notorious mistakes I can see is that you didn't use playergui than startergui because if you don't the code will function when the player has respawned causing errors and confusing the script. Also, use a not because it's quicker and a time saver:

local function open()
local player = game.Players.LocalPlayer
    local menu = player:WaitForChild( "PlayerGui" ).ScreenGui.menu
    local frame = player:WaitForChild( "PlayerGui").ScreenGui.Frame
    frame.Visible = true        
end

local function close()
    local menu = player:WaitForChild( "PlayerGui").ScreenGui.menu
    local frame = player:WaitForChild(" PlayerGui").ScreenGui.Frame
     frame.Visible =not frame.Visible
    end
end

local menu = player:WaitForChild( "PlayerGui").ScreenGui.menu
menu.MouseButton1Down:Connect(open())
menu.MouseButton1Down:Connect(close())
0
Just so you know, 'menu.MouseButton1Down:Connect(open())' that's incorrect. It would be 'menu.MouseButton1Down:Connect(open)'. Same thing for Close. xInfinityBear 1777 — 4y
0
Thanks for the help! Quick question, why are you using WaitForChild? Doesn't PlayerGui immediately load in? AtrocityPrevails 49 — 4y
0
No. Because if the player doesnt load in then it will cause a lot of issues JesseSong 3916 — 4y

Answer this question