✓ Accepted answer
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
local gui = ReplicatedStorage.ScreenGui:Clone()
gui.Parent = plr.PlayerGui
else
plr.PlayerGui:FindFirstChild("ScreenGui"):Destroy()
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
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)
4 comments
Just destroy it instead of doing nil
I tried this first, but still didn't work. Destroy worked in studio, but would not work in servers
Wait so are you clicking with a click detector or is this in a Gui using a LocalScript?
Clicking on a part with click detector, and then to close/destroy it I have a local script inside part of the gui