Trying to change multiple parts properties at once with For Loop

There is more than one solution for this post. Thank you to everyone.

when i do the for i = 0,1,.1 do loop it does one part at a time. how would i make it so that it does all the parts at once but there is a wait added to the increment?

The script below is before the solution where only one part would change transparency at a time.

local folder = game.Workspace.Folder

for i, v in pairs(folder:GetChildren()) do
	if v:isA("Part") then
		for i = 0, 1, .2 do
			v.Transparency = i
			wait(.5)
		end
	end
end
2 Likes
local folder = game.Workspace.Folder

for i = 0, 1, .2 do
	for i, v in pairs(folder:GetChildren()) do
		if v:isA("Part") then
			v.Transparency = i
			
		end
	end
wait(.5)
end

not tested

3 Likes

Yep, he’s got it. You just want to switch the for loops. Happy coding!

1 Like

i tried that and it still does one part at a time.

Oh, I see what you mean now. Ok, one sec. Let me figure this out. How many parts are in your folder?

local folder = game.Workspace.Folder

for i = 0, 1, .2 do
	for _, v in pairs(folder:GetChildren()) do
		if v:isA("BasePart") then
			v.Transparency = i
		end
	end
	wait(.5)
end

fixed it

2 Likes

Son of a bugger I was just about to post that.

2 Likes

He’s got your answer. It worked for me too. I just changed game.Workspace.Folder to script.Parent haha.

1 Like
local folder = game.Workspace.Folder

for i, v in pairs(folder:GetChildren()) do
	if v:isA("BasePart") then
		coroutine.wrap(function()
			for i = 0, 1, .2 do
				v.Transparency = i
				wait(.5)
			end
		end)()
	end
end
3 Likes

i actually ended up using this before i saw your post. but it tis the answer. here’s another way i did it as well is writing the for i = 0,1,.2 before the for i, v in pairs() but you can’t name i for both for loops or it won’t work. need to name them differently.

yeah i endedd up using this too. is there a way to put more than one solution?

You shouldn’t have a problem with using i in both loops unless you need to use the i from the pairs loop inside of the second for loop

1 Like

it was my bad. i think i read the way you put the for loops wrong. but yeah. i ended up using this methodd and a coroutine wrap.

you are right. i for the coroutine wrap works. but i’m probably just going to call it different anyways. the other method is where i for both for loops broke the script

You should not use coroutines like this, each coroutine creates a new virtual thread SIGNIFICANTLY slowing down Lua execution on your game.

1 Like

it wasn’t significant lol drama queen. but there’s no need for coroutine