Tool not destroying on touched

Hi, i’ve been trying to make a tool get destroyed when it gets in contact with a trash can model, however i have no idea how to implement this.

Here is what i have right now:

function onTouch(part, tool: Tool)
	local tool = part.Parent:FindFirstChildWhichIsA("Tool")
	if tool then
		tool:Destroy()
	end
end

script.Parent.destroypart.Touched:connect(onTouch)

There is no error being thrown, and i don’t know why the tools dropped on the trash can are not being destroyed.

Help appreciated.

1 Like

Hi, .Touched only returns 1 value, which is “part”. “tool” is nil as no other values go through .Touched
Nvm. Have you tried inputting prints to see if the event does indeed run, and see if you are indeed getting the tool?

why do it like this? it would cause problems. just check if part.Parent:IsA("Tool")

local tool = part.Parent
	if tool:IsA("Tool") then
		tool:Destroy()
	end

You’ve declared tool twice!

Here’s the correct code!

local function onTouch(Part: any, _) -- I've forgotten if we're required to parse the tool param
-- Bad intentation alert since I'm typing on the DevForum!
   local tool = part.Parent:FindFirstChildWhichIsA("Tool")
   if tool then
    tool:Destroy()
   end
end

script.Parent.destroypart.Touched:Connect(onTouch)

There seems to be a small issue in the function declaration. The function parameter tool: Tool should not be included. Here’s the corrected code:

I also replaced connect with Connect since it is the proper capitalization for the function in Lua.

function onTouch(part)
    local tool = part.Parent:FindFirstChildWhichIsA("Tool")
    if tool then
        tool:Destroy()
    end
end

script.Parent.destroypart.Touched:Connect(onTouch)

does that even do anything? i think the local overrides it.

Also, i remembered tools were recognized as a “Model” class.

script.Parent.Touched:Connect(function(hit: BasePart)
	if hit.Parent:IsA("Tool") then
		hit.Parent:Destroy()
	end
end)

Example File:
TrashExample.rbxl (44.8 KB)

It’s a better practice to not, just for readability!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.