Why is "child" nil?

local function onClick(player, child)
	print(player.Name.." clicked me")
	local AllModel = {}
	local function AttachMotor(GetModel, Part0)
		local Model = GetModel:Clone()
		local Motor6D = Instance.new("Motor6D")
		Motor6D.Part1 = Model.BasePart
		Motor6D.Part0 = Part0
		Motor6D.Parent = Part0
		Model.Parent = player.Character
		table.insert(AllModel, Model)
		table.insert(AllModel, Motor6D)
	end
	AttachMotor(
		child.BasePart, 
		player.Character["RightHand"]
	)
end



local folderWithTheObjs = game.Workspace.CaseModels


folderWithTheObjs.ChildAdded:Connect(function(child)

	if child:FindFirstChild("BasePart") then
		child.BasePart.PickUp.MouseClick:Connect(onClick, child)

	end
end)``` it says child is nil, how do I fix that?

If this is a LocalScript try using :WaitForChild(childName) instead of using periods for finding children.
Try looking into the onClick function.

Its a serverscript, unfortunately…

Can you show me the ouput?

I would be able to see where the error is.

Replace:

	if child:FindFirstChild("BasePart") then
		child.BasePart.PickUp.MouseClick:Connect(onClick, child)

	end

with:

	if child:FindFirstChild("BasePart") then
		child.BasePart.PickUp.MouseClick:Connect(function(player)
            onClick(player, child)
        end)
	end
2 Likes

To expand upon @Dervex’s post, events don’t work like that. If you want to send arguments to the function that’s being called, you have to create an anonymous function, and inside of it, call the function that you want to run, with the arguments.

-- Yes
RBXScriptSignal:Connect(function()
    Function(Argument1, Argument2)
end)
-- No
RBXScriptSignaL:Connect(Function, Argument1, Argument2)
1 Like