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 open a gui with visible = true on mouse click?

So far I have gotten a gui to open and to close but I have a problem. The script I used to open the gui doesn't work with my close script. My open script is

local clickDetector = script.Parent
local brickClickedEvent = game.ReplicatedStorage.Events.BrickClicked
local debounce = false

clickDetector.MouseClick:Connect(function(player)
	if not debounce then
		debounce = true
		brickClickedEvent:FireClient(player)
		wait(1)
		debounce = false
	end
end)

That works all fine and dandy until I try to close it. My close script is

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	player:WaitForChild("PlayerGui").ScreenGui.Frame.Visible = true
end)

How would I make it so that these two scripts work together and allow me to both open and close?

1 comment

0

Also tip, put your code in the squiggly line button thing these ~~

3 answers

You can just use an if statement, I will do it with a text button and you can incorporate it into yours.

script.Parent.MouseButton1Click:Connect(function()
if script.Parent.Parent.Frame.Visible == true then
script.Parent.Parent.Frame.Visible = false
else
script.Parent.Parent.Frame.Visible = true

If I helped please accept this answer, feel free to ask any questions!

Simple if statement

--Server
local clickDetector = script.Parent 
local brickClickedEvent = game.ReplicatedStorage.Events.BrickClicked 
local debounce = false

clickDetector.MouseClick:Connect(function(player)
if not debounce then
	debounce = true 
	brickClickedEvent:FireClient(player)  --Lets the client know the brick was clicked
	wait(1) 
	debounce = false
	end 
end)
--LocalScript
local brickClickedEvent = game.ReplicatedStorage.Events.BrickClicked 
brickClickedEvent.OnClientEvent:Connect(function() --Client detected the click
	local ui = game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame
	if ui.Visible then --If the ui is visible
		ui.Visible = false --Make it invisible
	else --it is isn't
		ui.Visible = true --Show it
	end
end)

Don't listen to zompsi, it's easier if you just do:

script.Parent.Activated:Connect(function()
	frame.Visible = not frame.Visible
end

might have an error, haven't done that in a while, but it is simpler than zompsi and is cross platform friendly, just having MouseButton1Click you would imagine just goes for the computer with a mouse

the frame.Visible = not frame.Visible should: do set the visible to the opposite of what it currently is so it would do exactly what zompsi's script does, but simpler

1 comment

0

btw, your explanation wasn't very good, you don't need an event or a click detector, just use an event function

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