Why am I getting error: 20:59:51.467 - Workspace.Script:13: attempt to index function with 'Destroy'

I have it so if you are holding a tool you can destroy a part, but it keeps giving me the error.
script:

function CoalOre()
	local Ore = Instance.new("Part",workspace)
	Ore.Anchored = true
	Ore.CFrame = CFrame.new(math.random(-50, 50), 1, math.random(-50, 50))
	Ore.Shape = Enum.PartType.Ball
	Ore.TopSurface = Enum.SurfaceType.Smooth
	Ore.BottomSurface= Enum.SurfaceType.Smooth
	Ore.Size = Vector3.new(3, 3, 3)
	Ore.BrickColor = BrickColor.new("Black")
	Ore.Material= Enum.Material.Pebble
	Ore.Touched:connect(function(p)
		if p.Parent.Name == "Coal Pickaxe" or "Handle" then
			Ore:Destroy()
		end
	end)
end
while wait(math.random(5,10)) do
	CoalOre()
end

Is this your full script? I can’t seem to replicate the error. Are there any specific steps you take before it happens?

I should also note your check p.Parent.Name == "Coal Pickaxe" or "Handle" doesn’t do what you want it to do. This will evaluate to true for every item that touches your part. What you want is p.Parent.Name == 'Coal Pickaxe' or p.Parent.Name == 'Handle'

1 Like

tThis is the full script, it just gives that error when I touch it with my pickaxe

Few things you should do is first do what Autterfly said. Also, when modifying properties, or just creating instances in general, you should use

local part=  Instance.new("Part")
part.Parent = workspace

Also for the touched event, you could just do

if p:IsDescendantOf(pickaxe) then

I don’t know why this is not working but try changing the “Ore” variable to "orePart’ it might be getting it confused with the function somehow.

Does your pickaxe tool have a script inside that also destroys the coal ore? Calling “Destroy” twice in two different scripts will throw this error because the 2nd Destroy will have nothing to remove from the game. If that’s the case, you can try to remove either script.

1 Like

It’s probably because this happened:

  1. Ore got touched
  2. Ore got touched
  3. First connection destroys the part
  4. Second connection sees that the part is already destroyed

These are my solutions:
Disconnect the function with :Disconnect()

local Connection
Connection = Ore.Touched:connect(function(p)
	if p.Parent.Name == "Coal Pickaxe" or "Handle" then
		Ore:Destroy()
        Connection:Disconnect()
	end
end)

Or you can sue Debris:

local DS = game:GetService("Debris")
Ore.Touched:connect(function(p)
	if p.Parent.Name == "Coal Pickaxe" or "Handle" then
		DS:AddItem(Ore, 0)
	end
end)

But I would recommend the disconnect function.

This is unnecessary. Instance::Destroy implicitly disconnects all connections, so this is not the issue. What likely happened was OP did something like

function Ore()

end

And the code ended up seeing it as a function and not an instance.

1 Like

There is no script in the pickaxe tool

Looking at the error. It means. You tried to index (table.something) a function but you can’t do that

Somehow Ore turned into a function. Try printing typeof(Ore) before destroying it

Also, using the second parameter in Instance.new is deprecated and you should parent your Instance last

Doing while wait(x) do ... end is also bad practice

Devforum post

The While-Wait-Do Idiom, by cntkillme: Addressing while wait() loops

1 Like

I found what the mistake was, I accidentally set it to destroy the function instead of the ore.
Now it is

CopperOre:Destroy()

so it destroyes the ore, instead of the function.

But isn’t the variable called Ore?

And CopperOre is the function

1 Like

I changed the script to

function CopperOres()
	local CopperOre = Instance.new("Part",workspace)
	CopperOre.Anchored = true
	CopperOre.CFrame = CFrame.new(math.random(-50, -30), 1, math.random(-50, -3))
	CopperOre.Shape = Enum.PartType.Ball
	CopperOre.TopSurface = Enum.SurfaceType.Smooth
	CopperOre.BottomSurface= Enum.SurfaceType.Smooth
	CopperOre.Size = Vector3.new(3, 3, 3)
	CopperOre.BrickColor = BrickColor.new("Copper")
	CopperOre.Material= Enum.Material.Pebble
	CopperOre.Touched:connect(function(p)
		if p.Parent.Parent.Name == "Copper Pickaxe" then
			CopperOre:Destroy()
		end
	end)
end
while wait(math.random(3,5)) do
	CopperOres()
end

and it seems to work and no errors