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

How and why did this 'for' loop get this many parts?

I was bored, so I decided to make a Part scanner script, and I did this test, instead of using + 1 for counting the parts, I tested + i and it printed out 183789, why and how did it get that many? o_e Here is the script;

local numParts = 0

local function partScan(part)
for i,v in pairs(part:GetChildren()) do
pcall(function() local testName = v.Name end)
if v:IsA("Part") then
numParts = numParts + i --This is the line that I used 'i' instead of '1'
end
partScan(v)
end
end

partScan(game.Workspace)
print(numParts)

1 answer

✓ Accepted answer

Well, if you add i, you're adding the index of that specific table of children. Let's say the hierarchy looks like this (pretend it makes logical sense; the parenthesis and numbers is the number actually added to your variable):

Part (+1)
	Part (+1)
	Part (+2)
		Part (+1)
		Part (+2)
		Part (+3)
	Part (+3)
	Part (+4)
		Part (+1)
		Part (+2)
			Part (+1)
	Part (+5)

Basically doing your function is like doing this: 1 + 1 + 2 + 1 + 2 + 3 + 3 + 4 + 1 + 2 + 1 + 5, which equals 26, which clearly there are actually only 12.

Now, let's look at what happens if it doesn't find a "Part" like your code looks for, assuming this is the exact order it searches the children in:

Part (+1)
	Part (+1)
	Model -- Not a part, so it's not adding it.
	Part (+3)
		Part (+1)
		Part (+2)
		Model
		Part (+4)
	Part (+4)
	Part (+5)
		Part (+1)
		Model
		Part (+3)
			Model
			Part (+2)
	Part (+6)

Basically doing your function is like doing this: 1 + 1 + 3 + 1 + 2 + 4 + 4 + 5 + 1 + 3 + 2 + 6, which equals 33, which clearly there are actually 12 parts, still.

Conclusion: So, just stick with + 1 instead of + i to make a part counter to get the correct count.

Hint: If you want to get parts, use :isA("BasePart") instead of :isA("Part") because ALL types of bricks are BasePart.

12 comments

0

So, its gathering a random amount of numbers from the Parts?

0

It's not random. It depends all on how many children each item has inside it.

0

I hope this isn't too confusing.

0

Ah, I see, it's kind of like labeling them all, like in your explanation its doing 'Part #1' and 'Part #2', and its counting the numbers '#1' and '#2', is that right? I'm not too sure, so I'm making sure I'm atleast a bit onTrack. :P

0

Yeah, that's the simple way of putting it. Except in tables, there are key-value pairs. Typically tables' key is a number, and it represents the location in the table numerically.

0

I have one more question, I've seen and used 'BasePart' before, but what does it do, or how does it work?

0

BasePart is just the inherited class of all Bricks. Basically ALL bricks have the same properties of the BasePart class. http://wiki.roblox.com/index.php?title=API:Class/BasePart All these properties, methods, etc. are inherited down to all types of bricks. You may see some familiar ones.

0

So, it can tell that 'WedgePart' types are Parts too? :o

0

Yes. WedgePart, SpawnLocation, Seat, Platform, FlagStand, Part, all are BaseParts, even Unions/NegateOperations.

0

Wow, you've been a big help man. :) I never knew all that before, I feel a bit silly now, haha! Thanks so much man. :)

0

No problem.

0

One last thing: http://wiki.roblox.com/index.php?title=API:Class_Reference If you go here, you can see what is considered what. For example, if you look for BasePart, everything under that (and tabbed in) is considered a part of that specific class. As you can see here, EVERYTHING is under "Instance" as everything inherits the Instance properties. So if you use the :isA method on one of those clas

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