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

My part isnt going invisible and visible again?

Asked by 7 years ago

I don't know why please help.

while wait() do 
    if script.Parent.Transparency == 0 then
        for transparency = 0.1, 0, 1 do 
            script.Parent.Transparency = transparency
        end 
    elseif script.Parent.Transparency == 1 then
        for transparency = -0.1, 1, 0 do

            script.Parent.Transparency = transparency
        end
    end
end

2 answers

Log in to vote
3
Answered by 7 years ago
Edited 7 years ago

Numeric for

In a numeric for loop, you provide a start and finish range of numbers (with an optional step variant, that tells you by how much to change the increment variable), to repeatedly execute it's body of code until it's met your destination.

Setting up your range of numbers is done in two ways: Min, Max, (+Step) or Max, Min, -Step. Notice the +Step is in parenthesis, as it's rendered optional when looping through a range of numbers from Min to Max, whereas looping through Max to Min requires a negative step.

Problem

First of all, in your first for loop, your step is greater than your destination:

for transparency = 0.1, 0, 1 do -- 0.1 is the starting point, 0 is the destination, and 1 is the step.
    script.Parent.Transparency = transparency
end 

So you're trying to say:

  • Start at 0.1

  • Go to 0

  • And increase 0.1 by 1 each iteration

Because of this, your for loop won't even run. It will immediately say "Oh, well we're trying to get to 0, starting at 0.1, but we're adding a positive number each time so this is just going to go on forever... I better not run this"


Now because of how binary calculates floating point numbers, saying something like for i = 1,0,-0.1 isn't entirely accurate. Instead what you should do, is use whole numbers instead, and divide the increment by that whole number to get the 1-0 or 0-1 range. Here's an example:

for i = 1,10 do
    script.Parent.Transparency = i/10 -- Dividing by the destination to get the percentage.
end

That will go from 1, to 10, but only changing the transparency by a difference of 1/10 each time. For doing this backwards (ranging from 1,0), you pretty much do the same process but with a negative step. Here's an example:

for i = 10,1,-1 do -- Go from 10, to 1, and subtract 1 each time till we get to 1.
    script.Parent.Transparency = i/10 -- Goes from 1 to 0 with a difference of 0.1.
end

The example above does the same thing as the former, just backwards. Implement this concept in your code, and you should have no problems with floating points numbers or step variants in your numeric for loops! If you have any questions, just let me know.

Ad
Log in to vote
2
Answered by 7 years ago

There are several problems here. The first is that you have the starting value and step swapped. The second is that you need to add a wait() to see the transparency effects (you may wish to change the speed). I also had a problem where it would stop after becoming transparent again, and so I changed the loops to use integers instead, which fixed it.

while wait() do 
    if script.Parent.Transparency == 0 then
        for transparency = 0, 10, 1 do
            script.Parent.Transparency = transparency / 10
            wait(0.1)
        end
    elseif script.Parent.Transparency == 1 then
        for transparency = 10, 0, -1 do
            script.Parent.Transparency = transparency / 10
            wait(0.1)
        end
    end
end

Answer this question