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

Is there a function that uses all the children in a parent?

Asked by 6 years ago

Is there a function similar to COUNT.IF in excel? what I mean is, is there a function (or a way if there isn't) of seeing if all the children complete a certain value

eg: if I run the function with an if statement it will be like if all the children are blue then execute this, I DO NOT MEAN GETCHILDREN OR GETDECENDENTS I want to know if all the parts are blue in a model then execute this.

I'm sorry I'm bad at explaining things

0
If I helped you please accept my answer. Otherwise comment to me how I can help you better. User#21908 42 — 6y

3 answers

Log in to vote
0
Answered by 6 years ago

To follow your example let us make a script that checks the color of every part in a model. Here is an example:

local model = script.ExampleModel
local listOfItemsThatFitCriterea = {}

for i,v in pairs(model:GetChildren()) do -- we are looking through all the children here

    -- put your if color == blue or whatever then statement in here
        table.insert(listOfItemsThatFitCriterea, v.Name)
        -- what this does is it fills the table with the names of the parts that fit our criterea.
    end
end

Hope this helps, if you have any additional questions post them in the comments below. Have a great day scripting!

Ad
Log in to vote
0
Answered by
sonuka 24
6 years ago
Edited 6 years ago

This function checks all the parts in a model that are blue:

local model =  the model you want to check
for i,v in pairs(model:GetDecendents()) do
if v:IsA("BasePart") and v.BrickColor == "Blue" then
-- do stuff
end
end
0
Line 3 would error. BrickColor is a BrickColor, a string is a string. Just wrap it in a BrickColor.new"Blue" User#19524 175 — 6y
Log in to vote
0
Answered by
chomboghai 2044 Moderation Voter Community Moderator
6 years ago

You would have to use :GetChildren(), then as long as you hit the first child that does not evaluate to true in your case, for example the child is green but you are checking if all children are blue, then just return the function.

function ()
    for _, child in pairs(part:GetChildren())
        if child ~= blue then
            return
        end
    end

    -- whatever you want to do if they are all blue here
end


Answer this question