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

How to select more than 1 object in a model trough scripts?

Asked by 9 years ago

Of course game.Workspace.(modelname).(PartName) would do it. But I have so many parts in one model and I want to make anchored false. I have way too many parts to go in the script 1 by 1 and I really don't wanna go trough them and have to change every single name. Is there a getallchildren function for parts or something

1 answer

Log in to vote
0
Answered by
nilVector 812 Moderation Voter
9 years ago

The for loop is useful for many situations like this. It's especially helpful when you want to go through all the children of a specific model, like you want to do right now. In your case, it would do something like this:

--Goes through all children of model
for i, part in pairs (game.Workspace.modelName:GetChildren()) do 
    --Makes sure you're not trying to anchor something that is not a part and resulting in an error
    if part:IsA("Part") 
        part.Anchored = false --Anchors that iterated part
    end
end

Not only does it save time, it's more efficient, for you're using less line of code.

However, keep in mind, if you want to loop through a table, you would have to use ipairs, not just pairs.

In addition, there is another type of for loop where you use i as the iterator_value. It is useful in some situations, but can sometimes be considered less efficient, because it only goes through the loop a set number of times. I personally find it useful when you don't know what the set number of times would be, as it is stored in a variable. Here's how a regular one would look like:

--This will print "Hello world!" 20 times
for i = 0, 20 do
    print("Hello world!") 
end
0
Thank you so much! ispeakspanish 5 — 9y
Ad

Answer this question