Hello! So. I have this script for a GUI that opens up the gui once a part is clicked, simply a click-for-gui script.
However, the problem comes in when the part is pressed and if you press it again, the same gui will appear over the existing gui and it can be clicked infinite times.
I’m not a good scripter so I hope that someone will be able to help me with this and tell me what to add in.
Here is the code:
function onClick(click)
for i,v in pairs (script.Parent:GetChildren()) do
if v.ClassName == "ScreenGui" then
c = v:Clone()
c.Parent = click.PlayerGui
end
end
end
script.Parent.ClickDetector.MouseClick:connect(onClick)
What you should do is check if the GUIs already exists in the player’s PlayerGui folder, and not clone them again if one is found.
You can add something like this before the actual cloning part of the script, which will just return nothing (stop the script) if it finds any of the GUI elements you wish to copy in.
for _,check in pairs(click.PlayerGui) do
if check == v then
return nil
end
end
You need to do a check to make sure it hasn’t already been activated
local ClickActive = false
function onClick(click)
if ClickActive == false then
ClickActive = true
for i,v in pairs (script.Parent:GetChildren()) do
if v.ClassName == "ScreenGui" then
c = v:Clone()
c.Parent = click.PlayerGui
end
end
end
end
You will also want to set ClickActive to be false once the gui is closed
local Clicked = false
function onClick(click)
for _,Screen in pairs(script.Parent:GetChildren()) do
if Screen:IsA("ScreenGui") then
if Clicked == false then
local c = Screen:Clone()
Clicked = true
c.Parent = click.PlayerGui
else
print("ERROR: Already clicked")
end
end
end
end
script.Parent.ClickDetector.MouseClick:connect(onClick)
When you close it, are you making the gui be destroyed or just turn invisible? If you are just making it invisible/disabling it, then that script will not allow it to open again, but if the close button destroys the script, then it should work.