FindFirstChild or other related functions stops the script if returned nil

As said in the title, any functions related to FindFirstChild( FindFirstChildWhichIsA etc.) stops the script if there is no such instance. Not only in one script, this happens on all my other scripts. An example is down below:

poorlad.Parent:FindFirstChildWhichIsA("Pants"):Destroy()
		poorlad.Parent:FindFirstChildWhichIsA("Shirt"):Destroy()
		for _, children in ipairs(poorlad.Parent:GetChildren()) do

			if children:IsA("Accessory") then
				children.Handle:FindFirstChildWhichIsA"SpecialMesh".TextureId = ""
				children.Handle.Color = Color3.new(1,1,1)

			end

		end

“poorlad” is the player who touched a part. If the player does not have shirts, pants or any accessories the script stops. Any way to fix this?

Thanks in advance.

If FindFirstChild returns nil, then you’re trying to call Destroy on nil.
It’s the same as nil:Destroy(), it will naturally return an error.
Also, your script doesn’t “stop”. It just errors, please pay more attention more attention to the errors next time.

1 Like

oh… I did not think about trying to destroy a nil instance gives an error

Much appreciated.

Not a nil instance but a nil value, which in Lua is representative of nothing (no value), as such when you attempt to call the instance method “:Destroy()” on it an error occurs as that method does not belong to it. Similarly if you attempted to call “:Destroy()” on any non-instance value (such as a number value) an error would also be raised/thrown.

Yeah, sorry, i’m a beginner developer and don’t know these value types that much…

1 Like