Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is is saying attempt to index nil with Touched?

Asked by 3 years ago
Edited 3 years ago

Here is my code

local model = workspace.Plots["1"].Building

model.ChildAdded:Connect(function()
    for name, child in pairs(model:GetChildren()) do
        child:FindFirstChild("Hitbox").Touched:Connect(function(hit)
            if hit.Parent ~= child then
                child:Destroy()
            end
        end)
    end
end)

The child is a model and it has a part under it called Hitbox but in the console it is saying attempt to index nil with Touched

0
typo(?) you said it has hitbox but in script there is a capitol "H" Heavenlyblobmaster 271 — 3y
0
typo(?) you said it has hitbox but in script there is a capitol "H" Heavenlyblobmaster 271 — 3y
0
yeah its actually called Hitbox lemme change that poop07393cfhi3n2222 4 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

inside ChildAdded, pass the child that gets created and make sure it is the hitbox and then use the touched event

ChildAdded:Connect(function(child)
--code
end)
0
In my other function exactly identical to this it works and I didn't put child in the parantheses. poop07393cfhi3n2222 4 — 3y
Ad

Answer this question