I have a Gui which is cloned into a player's playergui (with no issue) when a part is clicked. Then I have the following in the close button for this Gui (which also works):
script.Parent.MouseButton1Click:connect(function() script.Parent.Parent.Parent = nil end)
However, when trying to clone the Gui into PlayerGui for a second time after removing it once, it is not successful. Does anyone know why this might be?
Hope this helps you it worked for me!
On this script I made the click detector toggle the Gui, so you can always remove the else statement.
local ReplicatedStorage = game:GetService("ReplicatedStorage") script.Parent.ClickDetector.MouseClick:connect(function(plr) if not plr.PlayerGui:FindFirstChild("ScreenGui") then -- Change ScreenGui to Gui name local gui = ReplicatedStorage.ScreenGui:Clone() gui.Parent = plr.PlayerGui else plr.PlayerGui:FindFirstChild("ScreenGui"):Destroy() -- Again change ScreenGui to Gui name end end)
Use this one if you are toggling through the gui
local ReplicatedStorage = game:GetService("ReplicatedStorage") script.Parent.ClickDetector.MouseClick:connect(function(plr) if not plr.PlayerGui:FindFirstChild("ScreenGui") then -- Change ScreenGui to Gui name local gui = ReplicatedStorage.ScreenGui:Clone() gui.Parent = plr.PlayerGui end end)
For a local script to close the gui just do this
script.Parent.MouseButton1Click:connect(function() if game.Players.LocalPlayer.PlayerGui:FindFirstChild("ScreenGui") then game.Players.LocalPlayer.PlayerGui.ScreenGui:Destroy() end end)
Instead of setting to nil, find the gui you want to close and use the :Destroy() function on it instead.
for instance
script.Parent.Parent.Parent:Destroy()
this is if the script.Parent.Parent.Parent is the gui u want to destroy of course.