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

Looping Part in Workspace Without Calling the Individual Part?

I am wanting to do a loop of changing parts color without having to write each part out.

Such as, I have 5 bricks in workspace. I want to change them all with the loop. How would I go about this?

Example (script in workspace)

local part = script.Parent

x = 1

while x =< 5 do
	part[x].Color = Color.new(0,255,0)
	wait(2)
	x +=1
end

Don't know what this way is called.

1 answer

✓ Accepted answer

I think you are talking about a for - loop...the "-" variable is the number and you can specify how many times you want it to repeat, as well as the start and what increment you want to go by

For example, in your case, you would do

local part = script.Parent

-- you do not need to specify "x = 1" as it is already in the loop
for x = 1,5,1 do -- the start (1), the end (5), and the increment (go by 1)
	--[[ this next part i noticed some mistakes
	first, you say "Color.new(0,255,0)". Color does not exist. Maybe you meant "Color3.new(0,255,0)?

	second, the color you are stating is "0,255,0". I think you want to use "Color3.fromRGB", since Color3.new has a range from 0 to 1 and includes decimals, but Color3.fromRGB includes a range from 0 to 255

	but i could be wrong because maybe Color is a module script and you are accessing "new" from it, and maybe you do want 0,255,0 as a range from 0 to 1 but I will change it just in case
]]
	part[x].Color = Color3.fromRGB(0,255,0) -- change color
	wait(2) -- your wait()
	-- no need to add onto x since the loop automatically does that
end

Some links:

For - loop

Color3

Hope this helps

7 comments

1

I tried this and keep getting an error. It's saying that the part doesn't exist. I check capitalization and location, though every looks right.

0

Okay so I am assuming that the part's names are not numbers....try changing "local part = script.Parent" to "local part = script.Parent:GetChildren()"

1

They are numbers, my parts are named Part1, Part2 ... Part 5. Could this be messing up because I am trying to call them from a group? As in, the parts are grouped together in workspace (trying to make a 'click to build and watch it being built').

0

OH. Okay. Then change line 12 in my script to "part["Part"..x]. It looks for a child called "Part1", "Part2", "Part3", etc etc from combining the number x with "Part"

1

Actually, nevermind. Found the problem was I still had ":GetChildren()". Thank you for the help!

1

One last thing, how do I set this question into 'answered'?

0

It should be at the bottom of my question and comments, something along the lines of "Accept Answer"

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