Question about :FindFirstChild

So for example my code is

if not target:FindFirstChild("Chest") then -- A block

my question is, is it possible to find more than one child using this code?

yes

if target:FindFirstChild("1") and target:FindFirstChild("2") then

just add and/or then you can stack it

Not within the function itself, as it only takes 1 argument which is a string (to find an instance), as opposed to allowing an array of strings to find multiple instances at once.

Like @BirdieI90 described, you’d have to have multiple conditional statements in one line to achieve your desired goal.

1 Like
local SearchTerms = {"Term1", "Term2", "Term3", "Term4", "Term5"}

local Flag
for _, SearchTerm in ipairs(SearchTerms) do
	if Target:FindFirstChild(SearchTerm) then
		Flag = true
	end
end

if Flag then --Target contains one or more of the searched instances.
	
else --Target contains none of the searched instances.
	
end

You can avoid long conditional expressions with for constructs.

1 Like

This method is far more time consuming, as each time you will need to copy & paste all of that just do so something in one line.

Alternatively, you could run a function somewhat like FindFirstChild which is:

function FindFirstChildArray(ParentInstance, ArrayInstances)
	for _, v in pairs (ArrayInstances) do
		if ParentInstance:FindFirstChild(v) then
			return ParentInstance:FindFirstChild(v)
		end
	end
	return nil
end

Then we can make AND condition of first child.

function FindFirstChildANDArray(ParentInstance, ArrayInstances)
	local array,alen = {},0
	for i, v in pairs (ArrayInstances) do
		if ParentInstance:FindFirstChild(v) then
			array[i] ParentInstance:FindFirstChild(v)
			alen = alen+1
		end
	end
	return array,alen
end

It really depends on what you’re trying to achieve, if you’re just looking for one instance from a set of instances then one would simply add a break statement to the script I provided (after the Flag = true line), which might I add would be more performant than a function implementation (functions in Lua have comparitively bad overhead). If you’re looking for multiple instances then a for construct would be the way to go, if a:FindFirstChild(b) and a:FindFirstChild(c) and a:FindFirstChild(d) then looks very unappealing.