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

why is this button not working?

I am trying to get a button (script.Parent) to make a GUI appear or disappear when you click on it. Why is it not working?

local button = script.Parent
local gui = game.Lighting.ScreenGui

function onClick(p)
	if (gui.Parent == p.PlayerGui) then
		gui.Parent = game.Lighting
	end
	
	if (gui.Parent == game.Lighting) then
		gui.Parent = p.PlayerGui
	end
end

button.MouseButton1Click(onClick)

Thanks!

1 answer

✓ Accepted answer
local button = script.Parent
local gui = game.Lighting.ScreenGui
function onClick() --You didn't need the p
    if gui.Parent == script.Parent.Parent.Parent then -- basically: script->button->gui->playergui
        gui.Parent = game.Lighting
    else
	gui.Parent = script.Parent.Parent.Parent
end
end
button.MouseButton1Down:connect(onClick) --You needed the :connect to connect your function

--Also, don't use MouseButton1Click, use MouseButton1Down


This should work, sorry if it doesn't, my studio kept crashing and I couldn't test it.

Edit: I added an "else" to the if-then statement, so if the parent isn't PlayerGui, it'll change the gui's parent to PlayerGui.

3 comments

0

this will open the GUI, but not close it.

0

I edited it, try the new one. It should work.

0

Thanks! It works now.

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