Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Is there a more efficient way to do this?

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

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

4 comments

0

But there are MANY more parts in this model

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.

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 :)

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.

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

2 comments

0

As i said above theres many more children in the model

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.