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

Set Gui = nil, but when trying to clone it again, no success, anyone know why?

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?

4 comments

0

Just destroy it instead of doing nil

0

I tried this first, but still didn't work. Destroy worked in studio, but would not work in servers

0

Wait so are you clicking with a click detector or is this in a Gui using a LocalScript?

0

Clicking on a part with click detector, and then to close/destroy it I have a local script inside part of the gui

2 answers

✓ 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 -- 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)

6 comments

0

Thanks! Let me go try it and I'll let you know

0

No problem! :-) if it works just mark my answer as accepted!

0

It works, but before I accept it, I have 1 more question about it. When I do this, I have to click on the block twice after closing the Gui to reopen it. Why might this be? (and I'll accept even if you can't help here, just curious and would be helpful to fix!)

0

I updated my code on my answer and tested it works perfectly fine for me

0

its odd, I'm still having the issue where I have to click the part twice to reopen the Gui (but only in servers). I'll see if I can figure this out. Thanks so much again though

0

still no luck, any suggestion on what this could be caused by?

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.

2 comments

0

Thanks for the help, but I have tried this and it still doesn't work. (works in studio but not in servers). In servers, once again it cannot be added back when destroyed when I try it

0

can you post the rest of your code, maybe where you clone it to see if there is an error there?

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