Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Get Print-Only Object List?

This is bit hard to explain but... what method would i have to type to get a print-only version of the amount of objects a model has inside of it?

For instance, if i have a model in Workspace that contained 2 Parts, What would i type to get that value? Ty for the help. :)

2 answers

✓ Accepted answer

The best solution to this would be recursive, so that we count objects inside of sub-models (as opposed to just direct children of the model we are talking about).

Here is our recursive statement:

#objects in model = #objects as direct children + #objects in each child

function countParts(model)
	local number = 0;
	if model:IsA("BasePart") then
		number = 1;
	end
	for _,child in pairs(model:GetChildren()) do
		number = number + countParts(child);
	end
	return number;
end

If there was a model of 5 parts in the workspace called "thing", then we could get that 5 using:

countParts(workspace.Thing)

Just place this script into the model. This will work. :)

	local parts = 0
	local stuff = script.Parent:GetChildren()
		for i = 1, #stuff do
			if stuff[i].ClassName == "Part" then
			parts = parts + 1
			end
		end
	print("Parts: "..parts)

2 comments

0

:IsA("Part") is more robust; especially because you can use :IsA("BasePart") and also count SpawnLocations, TrussParts, WedgeParts, etc

0

Ah ok, thank you. :)

This discussion is preserved as it was. New answers, comments, and votes are closed.