Ive got an issue with my code

im trying to make a thing when you click something it dissapears but it doesn’t work

local Detector = script.Parent:WaitForChild("ClickDetector")

function onClicked()
	script.Parent.Parent:Destroy()
end

Detector.MouseClick:connect(onClicked)

1 Like

Can you show me all of your ode because I can’t find out the problem

uh i made a Perlin Noise Generation so thats the only other code

like

local noise = math.noise
local random = math.random
local abs = math.abs
local insert = table.insert
local newV3 = Vector3.new
local cf = CFrame.new
local cfAngles = CFrame.Angles
local color3fromRGB = Color3.fromRGB
local ipairs = ipairs
local setmetatable = setmetatable
local workspace = workspace
local grassMaterial = Enum.Material.Grass
local treeModel = game.ReplicatedStorage.Tree

local SEED = workspace.SEED.Value
local TERRAIN_SMOOTHNESS = 20
local HEIGHT_SCALE = 100
local WIDTH_SCALE = 12
local X, Z = 4, 4
local TREE_DENSITY = .06
local TREE_OFFSET = 10
local MIN_TREE_SPAWN_HEIGHT = -25
local MAX_TREE_SPAWN_HEIGHT = 20
local MOUNTAIN_HEIGHT = 20
local CHUNK_SCALE = { X = WIDTH_SCALE * (X - 1), Z = WIDTH_SCALE * (Z - 1) }

local draw3dTriangle = require(game.ReplicatedStorage.Draw3dTriangle)

local function chunkPositionGrid(chunkPosX, chunkPosZ)
	local posGrid = {}
	
	for x = 1, X do
		posGrid[x] = {}
		
		for z = 1, Z do
			posGrid[x][z] = newV3(
				CHUNK_SCALE.X * chunkPosX + x*WIDTH_SCALE,
				noise((x + (X-1) * chunkPosX)/TERRAIN_SMOOTHNESS, (z + (Z-1) * chunkPosZ)/TERRAIN_SMOOTHNESS, SEED) * HEIGHT_SCALE,
				CHUNK_SCALE.Z * chunkPosZ + z*WIDTH_SCALE
			)
			
			if posGrid[x][z].Y > MOUNTAIN_HEIGHT then
				local relativeMountainHeight = posGrid[x][z].Y - MOUNTAIN_HEIGHT
				posGrid[x][z] = posGrid[x][z] + (newV3(0, .75, 0) * relativeMountainHeight) + (10 * newV3(0, .25, 0))
			elseif posGrid[x][z].Y > MOUNTAIN_HEIGHT - 10 then
				local relativeMountainHeight = posGrid[x][z].Y - (MOUNTAIN_HEIGHT - 10)
				posGrid[x][z] = posGrid[x][z] + (newV3(0, .25, 0) * relativeMountainHeight)
			end
		end
	end
	
	return posGrid
end

local function paintGrass(part)
	part.Material = grassMaterial
	local absoluteHeight = abs(part.Position.Y) * 1.25
	part.Color = color3fromRGB(75 + absoluteHeight, 151 + absoluteHeight, 75 + absoluteHeight)
end

local function paintStone(part)
	part.Material = grassMaterial
	
	local relativeMountainHeight = part.Position.Y - MOUNTAIN_HEIGHT
	local baseColor = color3fromRGB(75 + MOUNTAIN_HEIGHT, 151 + MOUNTAIN_HEIGHT, 75 + MOUNTAIN_HEIGHT)
	local mountainColor = color3fromRGB(125, 125, 125)
	
	if relativeMountainHeight >= 50 then
		part.Color = mountainColor
	else
		part.Color = baseColor:lerp(mountainColor, relativeMountainHeight / 50)
	end
end

local function paintTreeLeaves(tree)
	for index, part in ipairs(tree:GetChildren()) do
		if part.Name == "Leaf" then
			part.Color = color3fromRGB(random(50, 100), random(126, 176), random(50, 100))
		end
	end
end

local function createTriangleTerrain(chunk)
	local posGrid = chunk.posGrid
	local instances = chunk.instances
	
	for x = 1, X do
		for z = 1, Z do
			if x+1 <= X and z+1 <= Z then
				local a = posGrid[x][z]
				local b = posGrid[x+1][z]
				local c = posGrid[x][z+1]
				local d = posGrid[x+1][z+1]
				
				local wedgeA, wedgeB = draw3dTriangle(a, b, c)
				local wedgeC, wedgeD = draw3dTriangle(b, c, d)
				
				for index, wedge in ipairs { wedgeA, wedgeB, wedgeC, wedgeD } do
					if wedge.Position.Y < MOUNTAIN_HEIGHT then
						paintGrass(wedge)
					else
						paintStone(wedge)
					end
				end
				
				insert(instances, wedgeA)
				insert(instances, wedgeB)
				insert(instances, wedgeC)
				insert(instances, wedgeD)
			end
		end
	end
end

local function spawnTrees(chunk)
	local posGrid = chunk.posGrid
	local chunkPosX = chunk.x
	local chunkPosZ = chunk.z
	local instances = chunk.instances
	
	for x = 1, X do
		for z = 1, Z do
			if x+1 <= X and z+1 <= Z then
				
				local height = posGrid[x][z].Y
				if height >= MIN_TREE_SPAWN_HEIGHT and height <= MAX_TREE_SPAWN_HEIGHT then
					
					local spawnModulo = noise((chunkPosX * x)/10, (chunkPosZ * z)/10, SEED * chunkPosX * chunkPosZ) % .1
					local spawnTree = spawnModulo < TREE_DENSITY and true or false
					if spawnTree then
						local tree = treeModel:Clone()
						
						paintTreeLeaves(tree)
						
						tree:SetPrimaryPartCFrame(cf(posGrid[x][z])
							* cfAngles(0, noise(x/7, z/7, SEED * 2) * 10, 0)
							* cf(noise(x*5.4, z*5.4, SEED*2) * TREE_OFFSET, 0, noise(x*5.4, z*5.4, SEED*2) * TREE_OFFSET)
						)
						
						tree.Parent = workspace
						
						insert(instances, tree)
					end
					
				end
				
			end
		end
	end
end

local Chunk = {}
Chunk.__index = Chunk

Chunk.WIDTH_SCALE = WIDTH_SCALE
Chunk.X = X
Chunk.Z = Z

function Chunk.new(chunkPosX, chunkPosZ)
	local instances = {}
	
	local chunk = {
		x = chunkPosX;
		z = chunkPosZ;
		instances = instances;
		posGrid = chunkPositionGrid(chunkPosX, chunkPosZ);
	}
	
	setmetatable(chunk, Chunk)
	
	createTriangleTerrain(chunk)
	spawnTrees(chunk)
	
	return chunk
end

function Chunk:Destroy()
	for index, instance in ipairs(self.instances) do
		instance:Destroy()
	end
end

return Chunk

Actually what I need is the objects in workspace. Like all the parents of the click detector

If you use :Connect or :connect it doesn’t matter it’ll still work.

Hey there, maybe try using a print?


local Detector = script.Parent:WaitForChild("ClickDetector")

function onClicked()
	script.Parent.Parent:Destroy()
        print("done")
end

Detector.MouseClick:connect(onClicked)

If you tried it check output and look for the ‘done’ if it’s not there then there’s probably something breaking it.

ok something is breaking it idk why

Try this:

local Detector = script.Parent:WaitForChild("ClickDetector")

function onClicked()
        wait()
	script.Parent.Parent:Destroy()
end

Detector.MouseClick:Connect(onClicked)

This will never work because the script is a child of the tree and once you destroy it, the script gets deleted and it won’t get a chance to print anything. Once you call Destroy() on the parent, the script will get destroyed as well.

But why don’t you just do:

local PartToDestroy = --put the part location here so you're sure that you've got the right one.

ClickDetector.MouseClick:Connect(function)
    PartToDestroy:Destroy()
end)
1 Like

So you should just try this:

local Detector = script.Parent:WaitForChild("ClickDetector")

function onClicked()
        wait()
	script.Parent.Parent:Destroy()
end

Detector.MouseClick:Connect(onClicked)

Do you specifically need the player to click the part? Because you could just put the ClickDetector directly inside the tree model and you would be able of clicking the whole model.

try this

local part = script.Parent.Parent
local objv = Instance.new("ObjectValue")
objv.Value = part
local CD = script.Parent.ClickDetector -- click detector
	
repeat wait(0.001) until part and objv.Value
	
CD.MouseClick:Connect(function()
	if part then
		part:Destroy()
	end
end)

idk it does not work

robloxapp-20210819-2232232.wmv (106.9 KB)
robloxapp-20210819-2232474.wmv (3.0 MB)

delete the script from the part and add this below the treemodel:clone() part in the script that creates the trees and change the name of the part with the clickdetector inside

	local cd = tree:FindFirstChild("Part Name With Clickdetector"):FindFirstChild("ClickDetector")
						
						if cd then
							cd.MouseClick:Connect(function()
								if tree then
									tree:Destroy()
								end
							end)
						end

yea but the i have multiple trees like when they generate

i know just try it and see if it works

so do i do like a

local tree = game.Workspace.tree