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

How can I modify multiple parts at a time?

Asked by 9 years ago

How can I tell a script to perform the same action on multiple bricks at a time? Without having to name and specify each individual part.

2 answers

Log in to vote
0
Answered by
yoshiegg6 176
9 years ago

Well, you can put them all in a model and use a for loop on them. It would work like this:

for i,v in pairs (game.Workspace.Model:GetChildren()) do --Do something to every part of the model
v.CanCollide = false

Edit: if they all have something in common, like all of them are red and no other bricks are that same color then you can iterate over the workspace and check if it's a part and it's color is red.

for i,v in pairs (game.Workspace:GetChildren()) do
if v:IsA("Part") and v.BrickColor = "Really Red" then
v.CanCollide = false

Do you want me to explain for loops?

Edit 2: Ok, for loops seem complicated at first, but they're simple once you understand them. For loops go over everything in a table, or anything in parentheses. Let me explain. Let's say you have a table of fruits that you like.

FavoriteFruits = {"Banana","Apple","Orange","Peach"}

Everything in a table has a key and a value. The key is the objects position in the table. Apple's key would be 2 because it's the second value in the table. Banana's would be 1, orange would be 3 etc.

So, using a for loop you can print each of these. The loop would once for every value in the table. It looks like this.

for i,v in pairs (FavoriteFruits) do

For just means it's a for loop. "i"represents the key of the value table. "v" represents the value of the thing in the table. in pairs means it goes over everything in the parentheses. (FavoriteFruits) is the name of the table thats getting looped. So, you're probably wondering: will it print all the values at once? Well, it actually loops once for every value in the table. Let me explain with a script.

FavoriteFruits = {"Banana","Apple","Orange","Peach"} --List of your favorite fruits in a table

 for i,v in pairs (FavoriteFruits) do --Goes over everything in the table
wait(3) --Wait so that it doesn't print them extremely quickly
print(v.."s are my number"..i.."favorite fruits.") 

So, we made a for loop going over favorite fruits. When it loops the first time its going to target the first part of the table, "Banana". So, the v value of Banana is Banana. printing v will return banana. Then put the dots to transition between strings and values. Then the string "s are my number" then it prints i, the key or position in the table of bananas. Then dots again and favorite fruits. So together it would print Bananas(value of first object) are my number 1(Key, position) favorite fruits. Then it would run a second time and target the next object in the table, apple. It would print Apples are my number 2 favorite fruits, and then Oranges are my number 3 favorite fruits and it would stop running after it targets the last object in the table. Final script:

FavoriteFruits = {"Banana","Apple","Orange","Peach"} --List of your favorite fruits in a table

 for i,v in pairs (FavoriteFruits) do --Goes over everything in the table
wait(3) --Wait so that it doesn't print them extremely quickly
print(v.."s are my number"..i.."favorite fruits.") 

Prints out: Bananas are my number 1 favorite fruits 3 seconds later Apples are my number 2 favorite fruits 3 seconds later Oranges are my number 3 favorite fruits 3 seconds later Peaches are my number 4 favorite fruits

Confusing?

0
Yeah, I could use some explanation for why loops. Thanks BloodyBooger 20 — 9y
0
Ok, I'll put it as an edit in my post. yoshiegg6 176 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

If you want to do two things at the same time in a script, then you use coroutines. If you want to edit multiple parts at the same time with the same properties, then put them in a model and edit them that way. If you don''t want to name each part but manipulate them separately, then use both. For example:

local parts = workspace.editParts:GetChildren() --where editParts is the model with the parts you want to change

local c =  {"Bright blue", "Bright red", "Bright yellow"} --table of colors

for i, v in ipairs(parts) do --iterate through the table

coroutine.resume(coroutine.create(function(part,colors) --start a thread with an anonymous function

while true do --repeat code

part.BrickColor = BrickColor.new(colors[math.random(1,#colors)]) --change color of the part to a random color

wait(math.random(10,50)/100)--wait a random amount of time between 0.1 and 0.5 before running the loop again

end --end the while loop

end) --end the function

,v, c) --pass the part & color table as arguments to the function
end--end the for loop

If you don't know about coroutines, you can read about them here.

Answer this question