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.
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?
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)