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

if variable isn't nil, then it trows that variable is nil ;( What I'm doing wrong?

29: if child ~= nil then child:FindFirstChild("Button").Touched:connect(function(plr)

  • ButtonHandler:29: attempt to index a nil value

2 answers

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().

2 comments

0

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?

0

You shouldn't throw in random Instance:WaitForChild() calls. If the child doesn't exist, just return.

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.