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 8 years ago

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

local par = script.Parent
modes = {"FallDown","Spiral","Generic","SlideOpen","BlockFall","CollumnFall","Firework","FallUp","CheckerBoard","FallingCheckerboard"}

while wait() do
    local picked = modes[math.random(1,#modes)]
    if picked == "FallDown" then -- Fall Down
        local color = BrickColor.Random()
        par.p1.BrickColor = color
        par.p2.BrickColor = color
        par.p3.BrickColor = color
        par.p4.BrickColor = color
        par.p5.BrickColor = color
        par.p6.BrickColor = color
        par.p7.BrickColor = color
        par.p8.BrickColor = color
        par.p9.BrickColor = color
        par.p10.BrickColor = color
        par.p11.BrickColor = color
        par.p12.BrickColor = color
    end
end

2 answers

Log in to vote
0
Answered by
Ryukiyo 65
8 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.

local par = script.Parent
modes = {"FallDown","Spiral","Generic","SlideOpen","BlockFall","CollumnFall","Firework","FallUp","CheckerBoard","FallingCheckerboard"}

while wait() do

    local picked = modes[math.random(1,#modes)]

    if picked == "FallDown" then -- Fall Down

        local color = BrickColor.Random()

    for _, v in pairs (par:GetChildren()) do
        if v:IsA("Part") or v:IsA("UnionOperation") then -- Checking to see if the child is a part / union, filters out any other instances
            v.BrickColor = color -- if part passes through check, change the brickcolor
        end
    end
    end
end

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 — 8y
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 — 8y
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 — 8y
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 — 8y
Ad
Log in to vote
0
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
8 years ago

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

local par = script.Parent
modes = {"FallDown","Spiral","Generic","SlideOpen","BlockFall","CollumnFall","Firework","FallUp","CheckerBoard","FallingCheckerboard"}

while wait() do
    local picked = modes[math.random(1,#modes)]
    if picked == "FallDown" then -- Fall Down
        local color = BrickColor.Random()
        for i, v in pairs(par:GetChildren()) do
            if v:IsA("Part") then
                v.BrickColor = color
            end
        end
    end
end
0
As i said above theres many more children in the model bubbaman73 143 — 8y
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 — 8y

Answer this question