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

How can I get 2 close buttons to work with each other simultaneously?

Asked by 3 years ago
Edited 3 years ago

For my UI menu I have 2 buttons. Button 1 is to open and close the menu, and button 2 is a close button on the menu to close the menu, however I am having some issues with this. This is because, for the script for button 1 I have this:

local open = false
codeButton.MouseButton1Up:Connect(function()
    GuiSound:Play()
    if open == false then
        openGui()
        open = true
    elseif open == true then
        closeGui()
        open = false
    end
end)

But when button 2 is pressed, and closes the menu, the script for button 1 still thinks the menu is open meaning I have to click button 1 twice to open the menu back up again because it thinks open = true, how would I fix this without the use of global variables?

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Don't use a variable for this when you can simply use the Visible property of your menu to check if it's open.

local guiObject = script.Parent
local button = script.Parent.Parent.Button
local Visible = guiObject.Visible

button.MouseButton1Click:Connect(function()
    if Visible == true then
        Visible = false -- or use your custom function
    else
        Visible = true -- or use your custom function
end)
Ad

Answer this question