Attempt to index nil with 'MouseClick'?

  1. What do you want to achieve? Keep it simple and clear!

  2. What is the issue? Include screenshots / videos if possible!

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

for _,v in pairs(workspace.Pollen:GetChildren()) do
	
	local detector = v:FindFirstChild("ClickDetector")
	
	detector.MouseClick:connect(function() -- line 5
		print("Clicked! ", detector.Parent.Name)
	end)
	
end

Could you provide a screenshot of what the ancestry looks like for the children of pollen? A couple possibilities just off the bat could be:

  • The ClickDetector isn’t named ClickDetector. (Can be remedied with :FindFirstChildWhichIsA(), or even better by using the correct name of the ClickDetector)
  • One or more of the parts (in this case, the children of “pollen”) don’t have a ClickDetector within it.

Although it’s not possible to prove without seeing the ancestry. It could be a couple other things as well although I believe this would make the most sense.

You should use :WaitForChild as it may not exist yet. Also you could use :GetDescendants for pollen to get all of its children in case the click detector is somewhere else.

1 Like

heres how to fix the code:

for _,v in pairs(workspace.Pollen:GetChildren()) do
	
	local detector = v:WaitForChild("ClickDetector", math.huge) -- math.huge is unnecessary but yeah I wanted to add it
	
	detector.MouseClick:connect(function() -- line 5
		print("Clicked! ", detector.Parent.Name)
	end)
	
end
1 Like
  • One or more of the parts (in this case, the children of “pollen”) don’t have a ClickDetector within it.
1 Like