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

How do I make a gui close button with gui enabled = false?

So far I have gotten a gui to open with gui enabled = true but how do I make one close with that same code.

local gui = script.Gui local brickClickedEvent = game.ReplicatedStorage.Events.BrickClicked

brickClickedEvent.OnClientEvent:Connect(function() gui.Enabled = true end)

1 answer

You said it yourself, you just set the enabled property to false. You'd do this by using a couple of if statements to check the state of the Gui (is it opened or closed?). If it's open, then close it, and vice versa

local gui = script.Gui 
local brickClickedEvent = game.ReplicatedStorage.Events.BrickClicked

brickClickedEvent.OnClientEvent:Connect(function() 
	if gui.Enabled = true then
		gui.Enabled = false
	elseif gui.Enabled = false then
		gui.Enabled = true
	end
end)

That code is fine, and it does its job. But why type all that when you can do it in fewer lines using the not operator?

local gui = script.Gui 
local brickClickedEvent = game.ReplicatedStorage.Events.BrickClicked

brickClickedEvent.OnClientEvent:Connect(function() 
	gui.Enabled = not gui.Enabled
end)

Essentially, the not operator inverts whatever it's operating on - true becomes false and false becomes true! Always be on the look out for where you can reduce your code and effort like this, although, don't obsess over doing this as it could impede the quality of your code. :)

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