✓ Accepted answer
A for loop works on each element. Your code says to do this:
- Hide A
- wait 2
- Show A
- wait 2
- Hide B
- wait 2
- Show B
- wait 2
- ....
It does what you wrote.
If you want to hide them all, wait 2, then show them all, then you should say that!:
while true do
for _, v in pairs(workspace.Part.SurfaceGui.Frame:GetChildren()) do
v.BackgroundTransparency = 1
end
wait(2)
for _, v in pairs(workspace.Part.SurfaceGui.Frame:GetChildren()) do
v.BackgroundTransparency = 0
end
wait(2)
end
A way to reduce the code-reuse and to let you do fancier things is to make a setTransparency function.
function setTransparency(t)
for _, v in pairs(workspace.Part.SurfaceGui.Frame:GetChildren()) do
v.BackgroundTransparency = t
end
end
Your loop then simply becomes
while true do
setTransparency(0)
wait(2)
setTransparency(1)
wait(2)
end
It's now even easier to do things like fading:
while true do
for t = 0, 1, 0.03 do
wait()
setTransparency(t)
end
end
Coroutines are not the solution to this. They consume many resources. Coroutines are designed to compute things cooperatively. They are not designed to do two things at the same time. They make code much harder to understand, debug, and write. Don't use coroutines.
if you insist on creating coroutines to do things in parallel, you should use spawn so that your intent is clear. Coroutines are very powerful, and if you're just creating new ones to do things in the background, you might as well save some typing as use spawn.
Two Previous Answers that discuss doing multiple things at once
2 comments
Remove the last wait function.
That makes no sense, all that does is make the next frame go visible and the last one go visible in a quicker manner. I need all of them to change at once.