I'm trying to make an image button open/close a frame when you click it. This is what I've tried but its not working:
function x() if game.StarterGui.Menu.MenuBackground.Visible == false then game.StarterGui.Menu.MenuBackground.Visible = true elseif game.StarterGui.Menu.MenuBackground.Visible == true then game.StarterGui.Menu.MenuBackground.Visible = false end end script.Parent.MouseButton1Click:connect (function (x) end)
You shouldn't be accessing StarterGui
. StarterGui is where GUIs are cloned from. You should be accessing the GUIs in the PlayerGui
for players to actually see them. Read this article for more info.
You're also connecting your function wrong. This is a correct example:
local function name() --// Code end name() --// Or, to use an event instance.EventName:Connect(name)
You should be using a LocalScript
if this is in the StarterGui, with the frame, and just access the GUIs from the script's parents instead of finding them how you are now.
local frame = script.Parent.Parent local guiButton = frame.Button local function closeFrame() frame.Visible = false end guiButton.MouseButton1Down:Connect(closeFrame)
As an example, a function that toggles the state of the frame would be this:
local frame = script.Parent.Parent local guiButton = frame.Button local function toggleFrame() frame.Visible = not frame.Visible end guiButton.MouseButton1Down:Connect(toggleFrame)