Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to perform a numeric loop within a generic loop?

Asked by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
9 years ago

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

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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

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 DigitalVeer 1473 — 9y
1
Oops, I missed the `wait` BlueTaslem 18071 — 9y
0
Thanks. Surprised I haven't thought of that Shawnyg 4330 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

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

0
Coroutines is really overkill in this situation. They also won't necessarily all be synced, so this isn't appropriate in all cases. BlueTaslem 18071 — 9y
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. DigitalVeer 1473 — 9y

Answer this question