Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Songist 49
6 years ago
Edited 6 years ago

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?

0
Just destroy it instead of doing nil YouSNICKER 131 — 6y
0
I tried this first, but still didn't work. Destroy worked in studio, but would not work in servers Songist 49 — 6y
0
Wait so are you clicking with a click detector or is this in a Gui using a LocalScript? YouSNICKER 131 — 6y
0
Clicking on a part with click detector, and then to close/destroy it I have a local script inside part of the gui Songist 49 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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)
0
Thanks! Let me go try it and I'll let you know Songist 49 — 6y
0
No problem! :-) if it works just mark my answer as accepted! YouSNICKER 131 — 6y
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!) Songist 49 — 6y
0
I updated my code on my answer and tested it works perfectly fine for me YouSNICKER 131 — 6y
View all comments (2 more)
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 Songist 49 — 6y
0
still no luck, any suggestion on what this could be caused by? Songist 49 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

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.

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 Songist 49 — 6y
0
can you post the rest of your code, maybe where you clone it to see if there is an error there? PoePoeCannon 519 — 6y

Answer this question