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

How would I reverse this script so that it will make the brick visible?

Asked by 7 years ago

Hi,

So I know that this below script will make it Transparent. But, how would I make it go from Transparent back to 0. I dont know how to switch the numbers around. Thanks,

for i = 0, 1, 0.1 do
   game.Workspace.Part.Transparency = i -- Make the Transparency of the part equal to i.
   wait(0.1)
end
1
for i = 1, 0, -0.1 Netflixy 126 — 7y

2 answers

Log in to vote
1
Answered by
Link150 1355 Badge of Merit Moderation Voter
7 years ago
Edited 7 years ago

About Loops

A loop is a statement, or chunk of code, that is executed multiple times. In Lua, there are three types of loops: for, while, and repeat loops.

The while loop will continue executing its body as long as its condition evaluates to true. The repeat loop is similar to the while loop in that it will continue to execute its body for as long as its condition is false. It is, in a sense, a reversed while loop. Unlike a while loop, the condition check will also occur at the end of a loop step, meaning a repeat loop's body will always execute at least once.

The last kind is the for loop. There are actually two different sub-kinds of for loops:

  • Numeric for loops; and
  • Generic for loops.

Today we will only see one of them, the numeric for loop.

The Numeric for Loop

A numeric for loop is like a while loop that keeps track of a counter variable. As long as the value of that counter variable is below a fixed limit, the loop continues.

It requires two arguments and can take an optional third argument:

  • The first argument is the initial value of the counter;
  • The second argument is the counter limit; and
  • The third argument is the "step" of the counter variable.

The step determines by how much the counter variable will increase (or decrease) after each step of the loop. If not given then the step argument will take the value 1 by default.

Here is the syntax for a numeric for loop:

for counter = init, limit, step do
    -- Do something...
end

And here is a simple example:

-- Count from 1 up to 100.
for i = 1, 99 do
    print(i)
end

About Your Problem

Your current loop starts from 0 and counts up to 1 in increments (or steps) of 0.1.

for i = 0, 1, 0.1 do
    game.Workspace.Part.Transparency = 1 - i

    wait(0.1)
end

If you want to reverse its behavior, then you have two solutions.

First, you can count down from 1 to 0 in steps of -0.1...

for i = 1, 0, -0.1 do
    game.Workspace.Part.Transparency = 1 - i

    wait(0.1)
end

Or alternatively, you can keep counting up, but instead subtract the counter's value from the limit and use the difference.

for i = 0, 1, 0.1 do
    game.Workspace.Part.Transparency = 1 - i

    wait(0.1)
end

Hope that helps!

0
I can't get my answer to format properly, for some reason. Link150 1355 — 7y
0
Looks fine to me :P OldPalHappy 1477 — 7y
Ad
Log in to vote
0
Answered by
nanaluk01 247 Moderation Voter
7 years ago

You would need to do this:

for i = 1,0,-0.1 do
    game.Workspace.Part.Transparency = i
    wait(0.1)
end

If this answer helped you, make sure you accept the answer! :-)

0
Please don't just share code; give an explanation as well. Not about how your code works, but rather why you proceed that way. Link150 1355 — 7y

Answer this question