When I play solo in Studio, after a few seconds the debugger kicks in, and tells me that there's an error in a script. It's a hat removing script parented to a part. When I touch it w/ my character, it removes all of my hats. Even though it does its job without any problems, I still get this error:
Error | attempt to index field 'Parent' (a nil value)
This is the code:
function onTouched(hit)
local d = hit.Parent:GetChildren()
for i=1, #d do
if (d[i].className == "Hat") then
d[i]:remove()
end
end
end
script.Parent.Touched:connect(onTouched)
Can any1 tell me what's wrong?

2 comments
remove is depreciated. Use Destroy() instead.
There is a difference in remove and destroy.