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

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

Asked by
Dwayder 29
8 years ago

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:

01function x()
02    if game.StarterGui.Menu.MenuBackground.Visible == false then
03        game.StarterGui.Menu.MenuBackground.Visible = true
04    elseif game.StarterGui.Menu.MenuBackground.Visible == true then
05        game.StarterGui.Menu.MenuBackground.Visible = false
06    end
07end
08 
09script.Parent.MouseButton1Click:connect (function (x)
10end)

1 answer

Log in to vote
5
Answered by 8 years ago
Edited 7 years ago

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:

1local function name()
2    --// Code
3end
4 
5name()
6--// Or, to use an event
7instance.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.

1local frame = script.Parent.Parent
2local guiButton = frame.Button
3 
4 
5local function closeFrame()
6    frame.Visible = false
7end
8 
9guiButton.MouseButton1Down:Connect(closeFrame)

As an example, a function that toggles the state of the frame would be this:

1local frame = script.Parent.Parent
2local guiButton = frame.Button
3 
4 
5local function toggleFrame()
6    frame.Visible = not frame.Visible
7end
8 
9guiButton.MouseButton1Down:Connect(toggleFrame)
Ad

Answer this question