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

Why won't my GUI destroy upon button activation?

I'm making a game that has a start menu in it. I want that GUI to disappear upon the player clicking the "Start Game" button, but it currently doesn't do anything when you click the button.

Here's my code:

local button = script.Parent
local toggled = false
local guiObj = ScreenGui 

local function onButtonActivated()
    if toggled == false then
        guiObj:Destroy()
    else
        button.Text = "Start Game"
        toggled = false
    end
end

button.Activated:Connect(onButtonActivated)

Any tips would be appreciated, thanks! :)

4 comments

0

Use .MouseButton1Click instead of Activated

0

So would it be button.MouseButton1Click:Connect(onButtonActivated) instead? Because I tried that and it didn't work.

0

You set guiObj equal to ScreenGui when ScreenGui is not declared.

0

The original code said "guiObj = nill" but they told me I should make a reference to the ScreenGui because that's where the button is declared.

2 answers

local button = script.Parent
local guiObj = game.StarterGui.ScreenGui

button.MouseButton1Click(
    function()
        guiObj:Destroy()
        button.Text = "Start Game"
    end
)

local button = script.Parent

local guiObj = ScreenGui 

local function onButtonActivated()
	if guiObj.Enabled == false then
		guiObj.Enabled = true
	elseif guiObj.Enabled == true then
		guiObj.Enabled = false
	end
end

button.Activated:Connect(onButtonActivated)

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