I am unable to get a ClickDetector to work

Hey everyone!
I have started working on a game recently where you can destroy and crops and wait for them to regenerate. However, I am facing issues getting the ClickDetector to work on the crop once it has regenerated. Is there some way that I can fix this problem?

Here is my code:

brick = script.Parent
local click = Instance.new("ClickDetector")
click.MaxActivationDistance = 10
click.Parent = brick

function Click()
	brick.Transparency = 1
	brick.ClickDetector:Destroy()
	if brick.Transparency == 1 then
		local random_amount = math.random(6, 15)
		print(random_amount)
		wait(random_amount)
		brick.Transparency = 0
		local click_detector = Instance.new("ClickDetector")
		click_detector.MaxActivationDistance = 10
		click_detector.Parent = brick
	end	
end
brick.ClickDetector.MouseClick:connect(Click)

Any help is appreciated!
Thank You.

This seems like an issue relating to something else, I copy pasted your code and it worked for me.

Are you using a server-script inside of a part?

If you’re referring to how the first time it works properly but the 2nd time nothing happens, you need to recreate the MouseClick event.

Though if you want to disable the ClickDetector until the crop regrows, can’t you set the MaxActivationDistance to 0 and then set it back to 10 after the crop has regrown instead of trying to make another ClickDetector?

Also I don’t tihnk you need the if brick.Transparency == 1 then if statement because you set the Transparency to 1 immediately when you click it

2 Likes