Need help iterating through children of selected model

Hello!

Essentially, I am working on a script that will allow me to iterate through the children of a selected model, find the TextureIDs of said children (assuming they are MeshParts), and printing it into Studio’s built-in output (the script in question is executed in the command line.)

I’ve run into a major roadblock though: I don’t know how I would go about iterating through EVERY part found in the model. I’ve attached my script below. Help would be much appreciated!

I am rather new to Roblox Lua, so please don’t be too harsh.

for _, selectedObj in next, game.Selection:Get() do
	if selectedObj:IsA("Model") then
		local children = selectedObj:GetChildren()
		if children[1]:IsA("MeshPart") then
			print(selectedObj, "Children have been found! It IS a MeshPart.", children[1].TextureID, "\n")
		else
			print(print(selectedObj, "Children have been found! It is NOT a MeshPart.", children[1].Name, "\n"))
		end
	else
		print("Fail!")
	end
end

Are we talking about children of children?

No. The script should ideally first compare one of the model’s children (in this case, a MeshPart) to the the condition if children[1]:IsA("MeshPart"). If it is true, it should then output the TextureID of said MeshPart. It should do this for EVERY part in the model.

Hmm, I’m a little confused on what you mean… However is this what you are looking for?

for _, selectedObj in next, game.Selection:Get() do
	if selectedObj:IsA("Model") then
		local children = selectedObj:GetChildren()
		for _,Child:Instance in children do
			if Child then
				if Child:IsA("MeshPart") then
					print(selectedObj, "Children have been found! It IS a MeshPart.", Child.TextureID, "\n")
				else
					print(selectedObj, "Children have been found! It is NOT a MeshPart.", Child.Name, "\n")
				end
			end
		end
	else
		print("Fail!")
	end
end

I’ve slightly modified your script to iterate through the ‘children’ table, instead of just checking the numeric key value of [1]

Allow me to be a bit more clear.

Essentially, I want the script to print the TextureID of all MeshParts within a model. This is more so a proof of concept than anything.

Your work-around is perfect though; it’s exactly what I was looking for.

Forgive me if it was unclear the first time around, I’m not really experienced with Lua.

Well, this code only works for children of the selected model, not any descendants(children of children)
Here’s a slightly modified version that you may want in the future

for _, selectedObj in next, game.Selection:Get() do
	if selectedObj:IsA("Model") then
		local children = selectedObj:GetDescendants()
		for _,Child:Instance in children do
			if Child then
				if Child:IsA("MeshPart") then
					print(selectedObj, "Children have been found! It IS a MeshPart.", Child.TextureID, "\n")
				else
					print(selectedObj, "Children have been found! It is NOT a MeshPart.", Child.Name, "\n")
				end
			end
		end
	else
		print("Fail!")
	end
end

If you’re confused about what descendants and children are, I suggest reading Forum Link: What are descendants and ancestors?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.