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

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

Log in to vote
5
Answered by 7 years ago
Edited 6 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:

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

Answer this question