Your Problem
When you index 'className' on line 4. you're probably getting an error like Attempt to index a nil value. This happens because the property you're trying to index is case sensitive, meaning you must get all the capitalization exactly right. The proper way to say it is ClassName.
Efficiency Fixes
Instead of indexing the ClassName Property, use the IsA function.
Check if there's a Humanoid in hit.Parent, that way you know it's a player that hit the object, and the event isn't firing when a part or something hits the object.
Use a generic for loop with the pairs function, as it's meant to iterate through tables.
Use the Destroy function!! Remove is deprecated
Your Code
function onTouched(hit)
local d = hit.Parent:GetChildren()
for i = 1, #d do
if d[i].ClassName == "Hat" then
d[i]:Destroy()
end
end
end
script.Parent.Touched:connect(onTouched)
Efficiency Edited Code
function onTouched(hit)
if hit.Parent:FindFirstChild('Humanoid') then
for i.v in pairs(hit.Parent:GetChildren()) do
if v:IsA('Hat') then
v:Destroy()
end
end
end
end
script.Parent.Touched:connect(onTouched)
1 comment
Thanks for the answer, but Shawnyg has less rep so.. I am going to accept his answer. But i appreciate both of yours :D