Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you Compare Children with the Same Name?

Asked by 9 years ago
Let's say I have several blocks with the name "Block"

and I want to compare their sizes. How would I use the :GetChildren() method in order to compare each and every brick to each other?

Let's also assume that the blocks can be destroyed or be added  at any time so I don't actually know how many blocks there are at any given time and I want to find the block with the largest Size.

This means I will have to count the Children (in a loop) and then compare each child's size to each other until I find the child with the largest Size. That brick whose size is larger than all of the other children would be renamed "LargestBrick"

0
Please use a blockquote next time... Muoshuu 580 — 9y

1 answer

Log in to vote
2
Answered by
dyler3 1510 Moderation Voter
9 years ago

In order to do this, you'll need either a while or a repeat loop to go through a for loop and look for the child constantly.


For a better explanation, let's look at this example code:

local GroupOfBricks = game.Workspace.GroupOfBricks --Gets the model that the bricks are in
local LargestBrick -- Variable we will use to store the largest brick
local LargestMass = nil --Variable we will use to store the largest brick size

while wait() do --loops continuously
    local Children = GroupOfBricks:GetChildren() --Gets the bricks
    for i, Brick in pairs(Children) do --Loops through all the bricks
        local Mass = Brick:GetMass() --Gets the size of the brick.
        if Mass > LargestMass then --Checks to see if the current brick is larger than the largest brick
            LargestMass = Mass -- Updates the mass variable
            Brick.Name = "LargestBrick"
            LargestBrick.Name = "Brick"
            LargestBrick = Brick -- Updates the largest brick variable
        end
    end
end 

You can either use this code and edit it to fit with your game, or you can write your own code similar to this. Anyways, if you do it correctly, I believe it should work.


Hopefully this helped you out. If not, or if you have any further problems / questions, please leave a comment below, and I'll see what I can do.

0
nice thank you SamDomino 65 — 9y
Ad

Answer this question