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

Can anyone help me with 2 for loops not repeating?

Asked by 9 years ago

I have a brick I want to go from almost 0 transparent to almost .8 in transparency over and over, can anyone help?

while true do
   for i = 0, .8, .01 do
    script.Parent.Transparency = i
    wait(.05)
    for i = .8, 0, .01 do
    script.Parent.Transparency = i
    wait(.05)
end
end
end

1 answer

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

Tab your code properly. That is vital for you to become a good scripter!

while true do
    for i = 0, .8, .01 do
        script.Parent.Transparency = i
        wait(.05)
        for i = .8, 0, .01 do
            script.Parent.Transparency = i
            wait(.05)
        end
    end
end

Notice how you have the second for loop inside the first one? That's not right -- you want the second one to happen after, not during it.

while true do
    for i = 0, .8, .01 do
        script.Parent.Transparency = i
        wait(.05)
    end
    for i = .8, 0, .01 do
        script.Parent.Transparency = i
        wait(.05)
    end
end

Now the first for loop will work as expected, but the second won't; .8 is larger than 0, but you're telling it to go up (+.01). You need to tell it to go down by giving a negative step:

while true do
    for i = 0, .8, .01 do
        script.Parent.Transparency = i
        wait(.05)
    end
    for i = .8, 0, -.01 do
        script.Parent.Transparency = i
        wait(.05)
    end
end
Ad

Answer this question