Gui script issue

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)

Parent the gui or a clone of the gui to the player outside of the function, then make the function set the Enabled property

1 Like

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

I’m kinda confused as I haven’t really done much scripting. Could you try to explain it to me more briefly, please?

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

Hmm. Strange, I keep pressing the part but the Gui doesn’t even appear

you have to reset ClickActive when the Gui is closed otherwise that will happen

This is a common checker used with tools and guis all the time so it would be handy for you to remember how it works.

Thank you.
But how do I reset ClickActive?

Its very simple

ClickActive = false

And that would reset it

I don’t know what i’m doing wrong, but it still won’t do anything

try using the Gui.Enabled property? You can toggle it on/off to make the gui visible/invisible

Try this:

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)
1 Like

Yea this seems to work. The Gui won’t show again after I close it, but I belive that it is just an issue with the gui. Thank you!

1 Like

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.