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

How to perform a numeric loop within a generic loop?

Hello! The script below functions fine, but I'm trying to figure out how to run the numeric loop at the same time, for all of the "TV's"? It changes the transparency from 1 to 0 gradually, but I would like it to do it to multiple parts at once. Any tips? Thanks!

for _,v in pairs(game.Workspace["TV's"]:GetChildren()) do
		for i=1,0,-0.1 do
			wait(0.1)
			v.Screen.Decal.Transparency = i
		end
	end

2 answers

✓ Accepted answer

It's pretty simple. Right now, you say 'for each TV, set the transparency to 1, 0.9, 0.8, ..., 0'.

What you mean to say is 'Set the transparency of each TV to 1, 0.9, 0.8, ...'

In other words, your loops are "inside out":

for i = 1, 0, -0.1 do
	wait(0.1)
	for _, v in pairs(game.Workspace["TV's"]:GetChildren()) do
		v.Screen.Decal.Transparency = i
	end
end

EDIT: I missed moving the wait -- you pause between changes in transparencies, not in switching between TVs

3 comments

1

Although you are looping for every part, OP wanted to know how to make it change transparency at the same time, but this solution still only changes one at a time

1

Oops, I missed the wait

0

Thanks. Surprised I haven't thought of that

Spawn

Roblox has a function named 'Spawn' that allows you to execute a code segment in a protected threat. It is one of the coroutine based functions that Roblox has built-in. It is basically just like running another script

Using spawn, we can make each part change transparency at the same time [or as close as possible]:

for _,v in pairs(workspace["TV's"]:GetChildren()) do
spawn(function()
for transparency = 1,0,-.1 do
v.Transparency = transparency
wait(.1)
end
end)
end

Thus, this creates a new thread for each direct child under 'TVs' which will make sure the transparency is set!

Useful Links

http://wiki.roblox.com/index.php?title=Function_dump/Functions_specific_to_ROBLOX#Spawn

http://wiki.roblox.com/index.php?title=Loops#For

http://wiki.roblox.com/index.php?title=Function_dump/Coroutine_manipulation

2 comments

0

Coroutines is really overkill in this situation. They also won't necessarily all be synced, so this isn't appropriate in all cases.

0

I understand that. I saw you posted on how to do it just by switching the loops, so I just wanted to show an alternative way incase perhaps a lot more needs to be done separately. But yes, it is an overkill for something as simple as this.

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