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