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

Is there a more efficient way to do this?

Asked by 9 years ago

Please god tell me theres a more efficient way to do this

01local par = script.Parent
02modes = {"FallDown","Spiral","Generic","SlideOpen","BlockFall","CollumnFall","Firework","FallUp","CheckerBoard","FallingCheckerboard"}
03 
04while wait() do
05    local picked = modes[math.random(1,#modes)]
06    if picked == "FallDown" then -- Fall Down
07        local color = BrickColor.Random()
08        par.p1.BrickColor = color
09        par.p2.BrickColor = color
10        par.p3.BrickColor = color
11        par.p4.BrickColor = color
12        par.p5.BrickColor = color
13        par.p6.BrickColor = color
14        par.p7.BrickColor = color
15        par.p8.BrickColor = color
View all 21 lines...

2 answers

Log in to vote
0
Answered by
Ryukiyo 65
9 years ago

You can use a for loop to quickly iterate over the children of "par" to save yourself some time by not having to struggle with changing each child's brickcolor separately.

01local par = script.Parent
02modes = {"FallDown","Spiral","Generic","SlideOpen","BlockFall","CollumnFall","Firework","FallUp","CheckerBoard","FallingCheckerboard"}
03 
04while wait() do
05 
06    local picked = modes[math.random(1,#modes)]
07 
08    if picked == "FallDown" then -- Fall Down
09 
10        local color = BrickColor.Random()
11 
12    for _, v in pairs (par:GetChildren()) do
13        if v:IsA("Part") or v:IsA("UnionOperation") then -- Checking to see if the child is a part / union, filters out any other instances
14            v.BrickColor = color -- if part passes through check, change the brickcolor
15        end
16    end
17    end
18end

Questions / comments? Feel free to message me and I'll answer them as soon as possible. If you are satisfied with the answer I provided, please upvote me. Thanks! - Kyo

0
But there are MANY more parts in this model bubbaman73 143 — 9y
0
If 'par' is your entire model, the for loop will take care of all the children that are of 'parts / unions' classes regardless of how many there are- which is why using for loops is efficient and time-saving. Ryukiyo 65 — 9y
0
He means there are a lot of parts that don't need to be colored in the model, I'll let you add that if statement :) nicemike40 486 — 9y
0
Oooh, I see. I was confused as to what he was trying to convey since the details wasn't specific enough for me. In that case, he could put the bricks he wants recolored in a model or table of their own and work from there. Ryukiyo 65 — 9y
Ad
Log in to vote
0
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
9 years ago

You could loop through 'par' and change the children.

01local par = script.Parent
02modes = {"FallDown","Spiral","Generic","SlideOpen","BlockFall","CollumnFall","Firework","FallUp","CheckerBoard","FallingCheckerboard"}
03 
04while wait() do
05    local picked = modes[math.random(1,#modes)]
06    if picked == "FallDown" then -- Fall Down
07        local color = BrickColor.Random()
08        for i, v in pairs(par:GetChildren()) do
09            if v:IsA("Part") then
10                v.BrickColor = color
11            end
12        end
13    end
14end
0
As i said above theres many more children in the model bubbaman73 143 — 9y
0
Well, assuming all the ones you want to change start with p, then you could add a check if it starts with p. Otherwise, you could have a value in each one you want to change, and only change it if that value is present. Pyrondon 2089 — 9y

Answer this question