Script does not work after ClickDetector is being inserted after deletion

  1. What do you want to achieve?
    I’m making a script inside a part that has a ClickDetector, that when the part gets clicked on, then the part’s transparency goes to 1 and CanCollide will be false and ClickDetector will be destroyed for 10 seconds, then everything goes back to normal and ClickDetector gets inserted to the same part again.

  2. What is the issue?
    The script works fine at first but when the ClickDetector gets inserted back (with Instance.new) to the part (and I have checked that it goes back to the right place), then when I click on it again, it does not work. The part does not disappear anymore, ClickDetector does not get destroyed and script just does not work.

  3. What solutions have you tried so far?
    Well at first I had the script inside the ClickDetector and I made it change parents. It worked but had the same issue as now. Then I decided that it was easier when the script was inside the part already. I’ve tried to use wait() but it also does not work.

If anyone could help me or give any tips, then it would really be appreciated!

Could you show the code so we can help since it’s a little hard when you explain it without code examples.

Use debounce when the clickdetector is clicked

1 Like

I also think what might be wrong is that the ClickDetector from before is still being used in the function when the part is clicked. You can fix this by adding a variable and assigning it to the original ClickDetector. When a new one is created, assign it to that one. In the MouseClick function, change it to [variable].MouseClick instead.

I think your problem is you are referencing your ClickDetector as a variable
local ClickDetector = part.ClickDetector

So, when it is destroyed the variable doesn’t update to the new one.

By the way you’re saying it, I am assuming that the connection is breaking, what I would do, is either after you use “Instance.new”, you would disable and enable the script (so the connection is remade to the different ClickDetector and not the old one that was gotten rid of), or put the script in the ClickDetector rather and use :Clone() in a separate script cloning it into the part (whichever is easier I suppose).

1 Like

This issue can be solved with a simple debounce statement. Lets say you have our part. In a script say:

local debounce = false

script.Parent.ClickDetector.MouseClick:Connect(function()
if not debounce then
debounce = true
script.Parent.Transparency = 1
script.Parent.CanCollide = false
wait(10)
debounce = false
end)

Can’t you just set the clickDetector’s parent to nil, and then when you need it back, just redefine the new parent?

local cd = script.Parent.ClickDetector

--instead of destroying the clickDetector:
cd.Parent = nil

--instead of instance.new:
cd.Parent = script.Parent

I do not think debounce is the problem here (unless they forgot to set the debounce back to false), otherwise it would most likely still work and there wouldn’t be much of a problem.

He wants to make it so there is no mouse “click” thing when they hover over it I am assuming, doing this will only take care of the variable and not to what it is intended to do. Although I can also assume he could (instead of destroying it), set “ClickDetector.MaxActivationDistance = 0” so they can’t click it and there would be no problem with the script as well.

This should actually fully solve your issue, as the most probably cause of the issue is what object you are calling in your variable. If you are calling the variable of the clickDetector outside of the function, then the variable will eventually be assigned to a destroyed part. Therefore, when you add in the new clickDetector, there is no variable for it. So, you could do my aforementioned solution, or you could call the variable inside the function.

True, but I would rather go with not destroying it at all though as it makes things easier imo?

I have now created a new and improved script that i tested and worked.

Here it is:
local debounce = false

script.Parent.ClickDetector.MouseClick:Connect(function()
if not debounce then
debounce = true
script.Parent.Transparency = 1
script.Parent.CanCollide = false
script.Parent.ClickDetector.MaxActivationDistance = 0
wait(10)
debounce = false
script.Parent.Transparency = 0
script.Parent.CanCollide = true
script.Parent.ClickDetector.MaxActivationDistance = 32
end
end)

now it doesnt show the mouse when clicked, the part will dissapear and reappear after ten seconds, and then the click icon will reappear as well.

Yes, then my example would work perfectly for your scenario, just change the parent.

My work here is done. Drop the mic :microphone: !

Yea but debounce itself wasn’t the problem is what I was saying, it was destroying the ClickDetector, and in the example you did it would work since you’re not destroying the ClickDetector and rather changing its MaxActivationDistance (Like I had mentioned earlier).

Actually after testing without debounce it doesn’t work.

It depends on how you write the code, in your case you do want a debounce or it could break if you click it multiple times, if you didn’t have the wait(10) you wouldn’t (or shouldn’t at least) need a debounce.

Unless you wanted a debounce of course-