Clickdetector inside part not working when cloned with script

I have a script(vegetationspawner) that clones parts with clickdetector inside it to workspace with Script inside Server Script Service. I also have a Script(partObtainer) inside Server Script Service that loop through all Mesh in a Folder in workspace that has a descendant of Click Detector. The partObtainer is only detecting the parts when the game loaded, Not the cloned one. I searched online but I didn’t find anything that helped.

VegetationSpawner script

repeat wait(1) until game.Workspace:FindFirstChild("TerrainObjects")

local tiles = game.Workspace.TerrainObjects
local Natureloc = game.ServerStorage.natureparts

local function respawner()
	
	for i, v in pairs(tiles:GetChildren()) do
		if v.Name == "land" then
			if #v:GetChildren() > 0 then continue end	
			local rate = math.random(1, 100) 
			if rate >=1 and rate <= 30 then -- TREE
				local treerate = math.random(1,100)
				if treerate >= 1 and treerate <= 20 then
					local nature = Natureloc.trees:GetChildren()[math.random(1, #Natureloc.trees:GetChildren())]:Clone()

					nature.CFrame = CFrame.new(v.Position) + Vector3.new(math.random(-v.size.X/2, v.size.X/2),nature.Size.Y/2 + v.Size.Y/2,math.random(-v.size.Z/2, v.size.Z/2))
					nature.Parent = v

				end
				--// THE VEGETATION
			elseif rate >= 31 and rate <= 60 then -- Vegetation
				local plantrate = math.random(1, 100)
				if plantrate >= 1 and plantrate <= 20 then --Carrot

					local patchclone = Natureloc.vegetation.carrotpatch:Clone()
					patchclone.Parent = v
					patchclone.CFrame = v.CFrame * CFrame.new(math.random(-v.size.X/2, v.size.X/2) / 2 ,patchclone.size.Y/2 + v.size.Y/2,math.random(-v.size.Z/2, v.size.Z/2) / 2)

				elseif plantrate >= 21 and plantrate <= 30 then --Berry

					local patchclone = Natureloc.vegetation.bush:Clone()
					patchclone.Parent = v
					patchclone.CFrame = v.CFrame * CFrame.new(math.random(-v.size.X/2, v.size.X/2) / 2 ,patchclone.size.Y/2 + v.size.Y/2,math.random(-v.size.Z/2, v.size.Z/2) / 2)
					patchclone.berry.CFrame = patchclone.CFrame
					--// 
				end
			end
			print(v.Name)
			
		end
	end	
end

game.Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
	if game.Lighting.ClockTime == 12 then
		respawner()
	end
end)

part Obtainer

local module = game.ServerStorage.Modules.partObtain
local parttoObtain = require(module)

repeat wait(1) until game.Workspace:FindFirstChild("TerrainObjects")

local descendants = game.Workspace.TerrainObjects:GetDescendants()

for index, descendant in pairs(descendants) do
	if descendant:IsA("ClickDetector") then
		descendant.MouseClick:Connect(function(player)
			parttoObtain.obtain(player, descendant.Parent)
		end)
		
	end
end

Everything is loaded to the server and made with script inside ServerScriptService.
All parts I used is a mesh because its easy to change the properties

You need to clone the mouse click connection. Add this to the part obtainer to connect the connection to the new descendant.

 game.Workspace.TerrainObjects.DescendantAdded:Connect(function(descendant)
	if descendant:IsA("ClickDetector") then
		descendant.MouseClick:Connect(function(player)
			parttoObtain.obtain(player, descendant.Parent)
		end)
	end
end)
1 Like