How would I make it so when you Click an object it will destory?

I’m currently starting off some experiments with a simulator, I’m currently stuck since right now when you go over the object (Touch it) it will be destroyed but I want it so that when you click it, it destroys instead?

Code:

local ClickDetector = script.Parent.ClickDetector
local Players = game:GetService("Players")

local farmChunk = script.Parent
local farmChunk2 = script.Parent.Parent.Head

local function onPartTouch(otherPart)
	
	local partParent = otherPart.Parent
	local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
	if humanoid then

		farmChunk:Destroy()
		farmChunk2:Destroy()
		
		local player = Players:GetPlayerFromCharacter(partParent)
		local leaderstats = player.leaderstats
		local cropStat = leaderstats and leaderstats:FindFirstChild("Crops")
		if cropStat then
			cropStat.Value = cropStat.Value + 1
		end
	end
end
farmChunk.Touched:Connect(onPartTouch)
farmChunk2.Touched:Connect(onPartTouch)

You can insert a click detector as an instance inside of the part. You can then use this function:

ClickDetector.MouseClick:Connect(function()
  -- do something
end)

The model gets destroyed, but the leaderstats don’t change and I also get an error…

Argument 1 missing or nil

local ClickDetector = script.Parent.ClickDetector
local Players = game:GetService("Players")

local farmChunk = script.Parent
local farmChunk2 = script.Parent.Parent.Head

ClickDetector.MouseClick:Connect(function()

		farmChunk:Destroy()
		farmChunk2:Destroy()

		local player = Players:GetPlayerFromCharacter()
		local leaderstats = player.leaderstats
		local cropStat = leaderstats and leaderstats:FindFirstChild("Crops")
		if cropStat then
			cropStat.Value = cropStat.Value + 1
		end
	end)

Destroy the objects after trying to give a point, also, you alreayd get the player when you use a click detector, the MouseClick event returns the player who clicked, use that

ClickDetector.MouseClick:Connect(function(player)
	local leaderstats = player:FindFirstChild("leaderstats")
	local cropStat = leaderstats and leaderstats:FindFirstChild("Crops")
	if cropStat then
		cropStat.Value += 1
	end
    farmChunk:Destroy()
	farmChunk2:Destroy()
end)
4 Likes

Thanks, now I can get back to work!

2 Likes

I was going to say to give the points before deletion, but then I made this script, oh well. If you want to make it so when you drag you mouse over the “farm land”, it has a selection box on it, and when your mouse leaves, it goes away, you can do something like this:

function SelectionBox(Part)
	Part.ClickDetector.MouseHoverEnter:Connect(function()
		local selBox = Instance.new("SelectionBox")
		local color = Part.Color
		
		local r, g, b = 0,0,0 --Inverting the parts color for the selection box

		r = 255 - (color.R*255)
		g = 255 - (color.G*255)
		b = 255 - (color.B*255)
		
		selBox.Adornee = Part
		selBox.Name = "selBox"
		selBox.Color3 = Color3.fromRGB(r,g,b)
		selBox.Parent = Part
	end)
	
	Part.ClickDetector.MouseHoverLeave:Connect(function()
		for i,v in pairs(Part:GetDescendants()) do
			if v:IsA("SelectionBox") and v.Name == "selBox" then
				v:Destroy()
			end
		end
	end)
end

May not work, not the best way to make something like this, but a decent draft model. Use this as a guide. Take everything with a grain of salt.

2 Likes