Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is is saying attempt to index nil with Touched?

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

3 comments

0

typo(?) you said it has hitbox but in script there is a capitol "H"

0

typo(?) you said it has hitbox but in script there is a capitol "H"

0

yeah its actually called Hitbox lemme change that

1 answer

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)

1 comment

0

In my other function exactly identical to this it works and I didn't put child in the parantheses.

This discussion is preserved as it was. New answers, comments, and votes are closed.