Descendants?;Recursion?

Hello! I am @Enchanted_Tix, and I was having a question or two. What is descendants and Recursion? How do I use it? I am really confused on what this is. For example, I am making it where if it has a descendant of a ObjectValue named like, “_Water” how will I detect it using “_Water”? Please answer my question(s).

Edit: How do I make it like where the script searches EVERY part of the game and checks if it has a value named, “_Water”?

Here is an article on descendants to help you out. I don’t focus too much on this instance, for I never really have a use for it, but I do know some things about it.

Article: Instance | Documentation - Roblox Creator Hub

descendants is basically all the children of whatever model you are referring to and the children of models/parts inside of that model you have referred to. Literally a continuous :GetChildren instance in one statement if you were to use the :GetDescendants instance.

for your extra question you can do a loop to search for the part your looking for and use the :GetChildren instance since the descendants one will not be a good use for your extra problem.

[TESTED]

Example:

for _, w in pairs(workspace:GetChildren()) do -- change workspace to the parent of "_Water"
    if w.Name == "_Water" then -- finds the child "_Water"
        -- code here
    end
end
1 Like

I’ll have a code example of exactly what you need in a few moments :slight_smile:

--Function to checkForWater
local function checkForWater(parent)

	--Iterate through all children (Descendants) of object
	for a, object in pairs(parent:GetChildren()) do
		
		--If object is a model or folder, call this function (recursion)
		if (object:IsA("Folder") or object:IsA("Model")) then
			checkForWater(object)
		else
			
			--If objece.Name is water, run your code here
			if (object.Name == "_Water") then
				
			end
		end
	end
end

So I know where your going, but is there a chance to check like EVERYWHERE like replicatedstorage? And in a model?

A descendant is every child instance that is directly under a parent instance.

If you want to search every part of the game, you’ll want to use the :GetDescendants() function to cycle through every instance if your game.

Here’s a little snippet:

for _,instance in ipairs(PARENT_INSTANCE:GetDescendants()) do
    if instance.Name == "_Water" then
        --logic here
    end
end

As for recursion, I don’t know much about it, but here’s a wikipedia article so take it as you will.

Hope this helped! :smiley:

well there is no feature for checking descendants in workspace, replicatedstorage, etc. in one line altogether, you would have to do this separately I believe.

Recursion is a type of loop in which a function calls itself until some sort of end condition is met.

Do game:GetDescendantts()
character threshold xd