Help with a click to show/hide Gui

Hello everyone. I made a nice GUI (imo) and I tried making it pop-up with a part and clickDetector. The problem is, when I click the close button in my GUI and try to re-open it with the same Part, it won’t open. I assume this is because the GUI is already cloned in PlayerGui. Would there be a way to fix this?

function Click(Player)
	if not Player.PlayerGui:FindFirstChild("Gui")then
	local gui = script.Gui:clone()
	gui.Parent = Player.PlayerGui
	gui.LocalScript.Disabled = false
	end
	end
script.Parent.ClickDetector.MouseClick:connect(Click)
2 Likes

Delete the cloned script in playergui

1 Like

How? With this code?

script.Parent.MouseButton1Click:Connect(function()
     script.Parent.Parent.Parent:Destroy()
end)

Why don’t you just fire a remote to the player to open the UI?

-- Server --
local Remote = -- Remove Event Here
Remote:Fire(player)
-- Client --
local Remote = -- Remove Event Here
Remote.OnClientEvent:Connect(function()
   -- Open UI here
end)

Try using this

function Click(Player)
       if not (Player.PlayerGui:FindFirstChild('AddedGui')) then
            local gui = script.Gui:Clone()
            gui.Parent = player.PlayerGui
            gui.LocalScript.Disabled = false     
       end
end

script.Parent.ClickDetector.MouseClick:Connect(Click)

Your forgot to use some of a bit basic things, like the parenthesis for using the not, and the ‘connect’ would kind of give an error.

Here is the script to toggle the gui. There are more efficient ways to do this, but yeah.

local gui = nil
function Click(Player)
    if not gui then
        gui = script.Gui:clone()
        gui.Parent = Player.PlayerGui
        gui.LocalScript:Destroy()
    else
        gui:Destroy()
    end
end

script.Parent.ClickDetector.MouseClick:Connect(Click)
1 Like

I see you’ve got your answer, but you would’ve done:

Player.PlayerGui.Gui:Destroy()

This is not a good solution. Don’t use remotes for everything, try to use as little as possible. UI should be done on the client unless you’re trying to be really secure.

I’m not sure how that isn’t a good solution. It’s the best way to communicate with the client from the server. I agree that the client should handle anything UI related but in this case, they are trying to get a UI to open from the server. If having fewer remotes is better, just have one Remote to handle UI communication with the server and vice versa.

Sorry, I meant fewer data. More remotes are better as it categorizes data for quicker efficiency.

But, using a server script for UI should be used instead, since MouseButton1Click runs on the server. What’s the point of doing remotes if you can just do that? What’s the point of doing this on the server in the first place?

Yeah, I totally agree, there’s no reason this should be done on the server anyway unless, of course, it’s for security like you mentioned before. The main goal is to have your server-sided scripts optimized, and not have it open/ closing UI unless it is absolutely needed. If the OP was to use remote events it could look something like this:

-- Server --
local RemoteEvent = ReplicatedStorage.RemoteEvent 
RemoteEvent:Fire(player, "UI_NAME") -- Send over the UI Name
-- Client --
local RemoteEvent = ReplicatedStorage.RemoteEvent 
local PlayerGui = Players.LocalPlayer.PlayerGui

RemoteEvent.OnClientEvent:Connect(function(uiName: string)
   local UI = PlayerGui:FindFirstChild(uiName)
   
   if not UI then 
      return warn(uiName.." couldn't be found") 
   end

   UI.Enabled = true
end)
1 Like