Attempted to index nil with “FindFirstChild”

Why is this error occurring?

  1. Occurs when the body part of the character is removed or disappeared.
local Players = game:GetService("Players")

local function ConnectLimbs(char)
	for i, limb in pairs(char:GetChildren()) do
		if limb:IsA("BasePart") then
			local atch0, atch1, atch2, atch3, atch4, atch5

			if limb.Name == "Head" then
				atch0 = char and char:FindFirstChild("Head"):FindFirstChild("NeckRigAttachment")
				atch1 = char and char:FindFirstChild("UpperTorso"):FindFirstChild("NeckRigAttachment")
			elseif limb.Name == "UpperTorso" then
				atch0 = char and char:FindFirstChild("UpperTorso"):FindFirstChild("WaistRigAttachment")
				atch1 = char and char:FindFirstChild("LowerTorso"):FindFirstChild("WaistRigAttachment")

				atch2 = char and char:FindFirstChild("UpperTorso"):FindFirstChild("LeftShoulderRigAttachment")
				atch3 = char and char:FindFirstChild("LeftUpperArm"):FindFirstChild("LeftShoulderRigAttachment")

				atch4 = char and char:FindFirstChild("UpperTorso"):FindFirstChild("RightShoulderRigAttachment")
				atch5 = char and char:FindFirstChild("RightUpperArm"):FindFirstChild("RightShoulderRigAttachment")
			elseif limb.Name == "LowerTorso" then
				atch0 = char and char:FindFirstChild("LowerTorso"):FindFirstChild("LeftHipRigAttachment")
				atch1 = char and char:FindFirstChild("LeftUpperLeg"):FindFirstChild("LeftHipRigAttachment")

				atch2 = char and char:FindFirstChild("LowerTorso"):FindFirstChild("RightHipRigAttachment")
				atch3 = char and char:FindFirstChild("RightUpperLeg"):FindFirstChild("RightHipRigAttachment")
			elseif limb.Name == "LeftUpperLeg" then
				atch0 = char and char:FindFirstChild("LeftUpperLeg"):FindFirstChild("LeftKneeRigAttachment")
				atch1 = char and char:FindFirstChild("LeftLowerLeg"):FindFirstChild("LeftKneeRigAttachment")
			elseif limb.Name == "RightUpperLeg" then
				atch0 = char and char:FindFirstChild("RightUpperLeg"):FindFirstChild("RightKneeRigAttachment")
				atch1 = char and char:FindFirstChild("RightLowerLeg"):FindFirstChild("RightKneeRigAttachment")
			elseif limb.Name == "LeftLowerLeg" then
				atch0 = char and char:FindFirstChild("LeftLowerLeg"):FindFirstChild("LeftAnkleRigAttachment")
				atch1 = char and char:FindFirstChild("LeftFoot"):FindFirstChild("LeftAnkleRigAttachment")
			elseif limb.Name == "RightLowerLeg" then
				atch0 = char and char:FindFirstChild("RightLowerLeg"):FindFirstChild("RightAnkleRigAttachment")
				atch1 = char and char:FindFirstChild("RightFoot"):FindFirstChild("RightAnkleRigAttachment")
			elseif limb.Name == "LeftUpperArm" then
				atch0 = char and char:FindFirstChild("LeftUpperArm"):FindFirstChild("LeftElbowRigAttachment")
				atch1 = char and char:FindFirstChild("LeftLowerArm"):FindFirstChild("LeftElbowRigAttachment")
			elseif limb.Name == "RightUpperArm" then
				atch0 = char and char:FindFirstChild("RightUpperArm"):FindFirstChild("RightElbowRigAttachment")
				atch1 = char and char:FindFirstChild("RightLowerArm"):FindFirstChild("RightElbowRigAttachment")
			elseif limb.Name == "LeftLowerArm" then
				atch0 = char and char:FindFirstChild("LeftLowerArm"):FindFirstChild("LeftWristRigAttachment")
				atch1 = char and char:FindFirstChild("LeftHand"):FindFirstChild("LeftWristRigAttachment")
			elseif limb.Name == "RightLowerArm" then
				atch0 = char and char:FindFirstChild("RightLowerArm"):FindFirstChild("RightWristRigAttachment")
				atch1 = char and char:FindFirstChild("RightHand"):FindFirstChild("RightWristRigAttachment")
			end

			if atch0 and atch1 then
				local ballInSocket = Instance.new("BallSocketConstraint", limb)
				ballInSocket.Attachment0 = atch0
				ballInSocket.Attachment1 = atch1
			end

			if atch2 and atch3 then
				local ballInSocket = Instance.new("BallSocketConstraint", limb)
				ballInSocket.Attachment0 = atch2
				ballInSocket.Attachment1 = atch3
			end

			if atch4 and atch5 then
				local ballInSocket = Instance.new("BallSocketConstraint", limb)
				ballInSocket.Attachment0 = atch4
				ballInSocket.Attachment1 = atch5
			end

		elseif limb:IsA("Accessory") then
			local h = limb:FindFirstChild("Handle")
			if h then
				local part0 = char and char:FindFirstChild("Head")

				local atch = h:FindFirstChildOfClass("Attachment")

				if atch.Name == "HatAttachment" or atch.Name == "FaceFrontAttachment" then
					part0 = char and char:FindFirstChild("Head")

					local fs = part0:FindFirstChild("face")

					if not fs then
						local decal  = Instance.new("Decal", part0)
						decal.Texture = "http://www.roblox.com/asset/?id=4841508676"
						return
					end

					if fs then
						fs:Destroy()
						local decal  = Instance.new("Decal", part0)
						decal.Texture = "http://www.roblox.com/asset/?id=4841508676"
					end
				end

				local ap = Instance.new("AlignPosition", part0)
				if part0 == char and char:FindFirstChild("Head") then
					ap.Attachment1 = char and char:FindFirstChild("Head"):FindFirstChild(atch.Name)
					ap.Attachment0 = atch
					ap.RigidityEnabled = true
					ap.Responsiveness = 70
				end

				local ao = Instance.new("AlignOrientation", part0)
				if part0 == char and char:FindFirstChild("Head") then
					ao.Attachment1 = char and char:FindFirstChild("Head"):FindFirstChild(atch.Name)
					ao.Attachment0 = atch
					ao.RigidityEnabled = true
					ao.Responsiveness = 70
				end
			end
		end
	end
end

Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local humanoid = char and char:FindFirstChildWhichIsA("Humanoid") or char and char:WaitForChild("Humanoid")
		humanoid.Died:Connect(function()
			ConnectLimbs(char)
		end)
	end)
end)
1 Like

Could you explain in more details what the issue is? I don’t really understand what you are asking.

v and v2-- is a bool, perhaps you should try or

Attracted to index nil with “Find First Child” is as follows.
It occurs when the character’s body part is removed or disappeared.

You mean sometimes it can’t find the character’s body part and generates an error? Try wrapping the whole thing in a pcall function

pcall(function()
	--blah
end)
1 Like

Like what @2kvigilanxe said. It is probs due to you doing the findfirstchild to find as an example the head and then due to that not existing when you try to find the next part (in the example given you find the head and then find the neckRigAttachment but due to there being no head you are trying to find the first child of nill.

What I suggest doing is checking before you find the diffrent things that the body still exists by doing a findfirstchild of something inside of the character like head and if it does not exist then don’t do the rest of the code but if it does then do the code.

1 Like

Yeah, something like

if something:FindFirstChild("HumanoidRootPart") then
   --blah
end