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

How to make an image button open and close a frame?

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)


1 answer

✓ Accepted answer

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)

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