im trying to make it so when the tools hitbox collides with a part, it grabs the parent of the part and gets children, the hitbox does work but whenever i try to get children it doesnt let me and fails to index with it. can anyone help me?
script.Parent.Hitbox.Touched:Connect(function(hit)
if hit.Name == "epichitbox" then
if not Debounce then
Debounce = true
local Flower = hit.Parent:GetChildren()
for i,v in pairs(Flower) do
if v:IsA("Model") then
if v:IsA("Part") then
v:Destroy()
end
end
end
print("Caught a flower!")
end
end
end)
Well you want to destroy the flower right? So instead of calling Destroy() on every single part, you could just call Destroy() on the flower model and Destroy() will automatically be called on all its children
well actually, it wont let me get the model by itself so thats the main reason why im using a for loop to just get the model since when i tried to get the model by itself it failed to index with it
what the, when i tried again it suddenly worked again? the model worked as intended, i have no idea what i did but it suddenly works. maybe its because i tried to do a for loop to destroy the model.
The reason you’re getting that indexing error is because of how your type checks are set up. You’re checking if something IsA("Model") and then immediately checking if it’s also a "Part", but an object can’t be both at once, so that part of the code never runs.
Here’s a cleaner version that should work fine:
local Debounce = false
script.Parent.Hitbox.Touched:Connect(function(hit)
if hit.Name == "epichitbox" then
if not Debounce then
Debounce = true
local parent = hit.Parent
if parent then
for _, v in pairs(parent:GetChildren()) do
if v:IsA("Part") then
v:Destroy()
end
end
print("Caught a flower!")
end
task.wait(0.5)
Debounce = false
end
end
end)