Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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!

1 comment

0

make sure that the code is in local script and also 'menu' is a screengui or a button?

3 answers

✓ Accepted answer

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)

2 comments

0

Hey could you explain the 'not', I understood everything apart from that part. Thanks!

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

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 :)

2 comments

0

Wrong use playergui rather than startergui and use a not

0

Hey that didn't really work but now I see the problem, thanks for trying to help anyway! :)

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())

3 comments

0

Just so you know, 'menu.MouseButton1Down:Connect(open())' that's incorrect. It would be 'menu.MouseButton1Down:Connect(open)'. Same thing for Close.

0

Thanks for the help! Quick question, why are you using WaitForChild? Doesn't PlayerGui immediately load in?

0

No. Because if the player doesnt load in then it will cause a lot of issues

This discussion is preserved as it was. New answers, comments, and votes are closed.