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

How should I repeat a "for loop"in a script instead of using a while loop?

Asked by 5 years ago
Edited 5 years ago

Right now I got

for i = 0, 10 do
2       script.Parent.Transparency = script.Parent.Transparency + 0.1
3       wait()
4   end

But as it is not a while loop and it is a for loop I want to repeat it, but how do I do this? I want it like to repeat immediately after each other. Thanks!

0
I don't use for loops and while loops when dealing with transparency, instead I use repeats. greatneil80 2647 — 5y

2 answers

Log in to vote
-1
Answered by 5 years ago
Edited 5 years ago

There are two different ways. First one is the suggested way and does not include while. Second one however, includes while. Still effective but not suggested. By the way first index in lua is 1. Start for loops from 1.

First one is basically calling a function whenever you want.

local function DoTransparencyThing()
    for i = 1,10 do
        wait()
        script.Parent.Transparency = script.Parent.Transparency + 0.1
    end
end

DoTransparencyThing()

So whenever the line "DoTransparencyThing()" runs, this function will run. However if you want to run this function over and over, you might need a while loop with a wait.

Now the while loop.

while true do
    for i = 1,10 do
        wait()
        script.Parent.Transparency = script.Parent.Transparency + 0.1
    end
    wait()
    script.Parent.Transparency = 0
end

See that wait() at the end? This is to let it wait before starting process again. Also making transparency 0 so it can be transparent with the loop again.

EXTRA

However, increasing a number value with for loop isn't the best way to do it. There are tweens which can do these without keeping you with a wait time I suggest you to research TweenService for more information.

Note: If this helped, can you upvote and accept as answer please? Upvote is very important for me, thanks <3

0
Don't remind users to accept your answers. It is nature for them to accept it. You're just being greedy. User#19524 175 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

Thanks!

Answer this question