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

How can I change i in a for loop back to it's original value?

Asked by
MemezyDev 172
4 years ago

I probably made no sense in the title, so I'll try to make it more clearer here. I have a fade script that fades text in and out,

for i = 1, 0, -0.1 do
    wait()
    brought.TextTransparency = i
end

I was wondering if I can redo the for but instead of it going to 0 I want it to go to 1. Is there any way to do this in the same for loop or do I have to make multiple?

0
i think u can't Nguyenlegiahung 1091 — 4y
0
You probably just have to make another loop going from 0-1 DarkDanny04 407 — 4y

3 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

It’s the same in terms of runtime if you had two for loops though...

So I don’t see why you would need to.

Also you can manipulate how much you want i to increase by if you don’t include the increment. So you could just say i + .1 in the for loop or i -.1.

Or you could do it recursively which is the fun way

local function ForLoop ( i, ending, incrementer, numberOfTimesItRanBackAndForth)
      if i == 1 then 
         incrementer = -.1
         if numberOfTimesItRanBackAndForth > 0 then 
             numberOfTimesItRanBackAndForth =  numberOfTimesItRanBackAndForth + 1
         end 
      elseif i == 0 then
         Incrementer = .1
         numberOfTimesItRanBackAndForth =  numberOfTimesItRanBackAndForth + 1
      end
      if numberOfTimesBackAndForth == 2 then
          return
      end

       wait()
       brought.TextTransparency = i

       i = i + incrementer
       return ForLoop(i, incrementer, numberOfTimesBackAndForth)
end

ForLoop(1, -.1, 0) 

Look over it and you can understand what it does. You can also set the number of times you want it to go to 0 to 1 and 1 to 0. So if numberOfTimesBackAndForth == 4 it would have went from 1 to 0, 0 to 1, 1 to 0 and 0 to 1.

You can do the same thing I did here in a while loop but without returning. You just have to add if statments and break when it hits a certain amount of loops you want.

Ad
Log in to vote
0
Answered by 4 years ago

You can make another for loop and put your loop for it to repeat, this is my only way that i can think.... For example:

for i = 1,2 do 
    for i = 1, 0, -0.1 do
            wait()
            brought.TextTransparency = i
    end
end

Pretty simple

Hope that helped !

Log in to vote
0
Answered by
Mayk728 855 Moderation Voter
4 years ago

I think this is what you're trying to do:

for i = 1, 0, -0.1 do
    wait()
    brought.TextTransparency = i
end
for i = 0, 1, 0.1 do
    wait()
    brought.TextTransparency = i
end

Answer this question