29: if child ~= nil then child:FindFirstChild("Button").Touched:connect(function(plr)
- ButtonHandler:29: attempt to index a nil value
29: if child ~= nil then child:FindFirstChild("Button").Touched:connect(function(plr)
You're using Instance:FindFirstChild() improperly. It will return nil if the child with the specified name does not exist. child.Button does not exist, so it will be nil, and you are attempting to index nil.Touched.
Additionally, RBXScriptSignal:connect() is deprecated, prefer RBXScriptSignal:Connect().
Well, I would assume that child is in fact not nil, but child:FindFirstChild("Button") is nil. Double check that.
Btw, you can do something like this:
if child and child:FindFirstChild("Button") then
child.Button.Touched:Connect(function(hit)
--stuff
end)
end
This discussion is preserved as it was. New answers, comments, and votes are closed.
2 comments
When I change it to WaitForChild() , then it doesn't trow error, but script freezes. Is there any way to make script ignore line if WaitForChild() can't find child?
You shouldn't throw in random Instance:WaitForChild() calls. If the child doesn't exist, just return.