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.
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.
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!
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.
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).
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.
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).
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.