Clicking detector enable/disable

Hello,

I need help with this.

function onClicked()

script.Parent.Parent.Screen.SurfaceGui.Enabled = false

script.Parent.Parent.Click.ClickDetector:Destroy()

wait(3)

script.Parent.Parent.Screen.SurfaceGui.Enabled = true

wait(3)

end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

So, I want it to do loop like detector disable then enable back when going on main Screen of GUI. I did put detector to destroy, I tried with archivable to be false as disabled.

I’d suggest making a variable that is a boolean. ex :

local isClicked = false

script.Parent.ClickDetector.MouseClick:Connect(function()
    if isClicked then return end
    isClicked = true
    -- code
    wait(time)
    isClicked = false
end)

I did but not working.

local isClicked = false

script.Parent.Parent.Click.ClickDetector.MouseClick:Connect(function()

if not isClicked then return end

isClicked = true

script.Parent.Parent.Screen.SurfaceGui.Enabled = false

wait(3)

script.Parent.Parent.Screen.SurfaceGui.Enabled = true

wait(5)

isClicked = false

end)

Um that script won’t do anything since return just end the function correct me if i’m wrong. And adding the bool value won’t help cause it is just a debounce.

About the problem, are there 2 click detectors cause this:

script.Parent.Parent.Click.ClickDetector

and this

script.Parent.ClickDetector

are different.

My wrong, correct the if statement

if isClicked then return end

Destroying click detectors and adding them is not the best solution.
1st you would need to do that on server
2nd everytime an object is created, packets are being sent

I didn’t said anything about that??

Apologizes, misunderstood a bit.