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

How to make GUI keep fading out and in ?

Asked by
Bulvyte 388 Moderation Voter
9 years ago

So umm i want this script just do fade in and fade out while true like its 0.50 background in the first time and then just goes to 1 transparency and 0.50 for the whole time, but it does not work! it just keeps going from 0.50 to -999

button = script.Parent

while true do
  for i = 1, 50 do
      button.BackgroundTransparency = button.BackgroundTransparency - 0.01
   wait(0.01)
if button.BackgroundTransparency == 0.50 then
     for i = 1, 50 do
      button.BackgroundTransparency = button.BackgroundTransparency + 0.01
   wait(0.01)
   end
  end
 end
end

thanks!

0
Anyone ?.. Bulvyte 388 — 9y
0
Bulvyte: if you're getting red lines on legobuildermaster's script, you need to hover over the script and click "Copy to clipboard" -- a button that appears at the top right of the script. Otherwise you'll still have the line #s, which Roblox doesn't accept. chess123mate 5873 — 9y
0
I did that.... heres a screenshot : http://prntscr.com/8aiygf but anyway im gonna see yours now Bulvyte 388 — 9y

2 answers

Log in to vote
1
Answered by 9 years ago

You should never check to see if a number with decimals is precisely equal to something because computer's often add decimals incorrectly. For instance, put this in the command bar and look at the output window: s = 0 for i = 1, 1000 do s = s + 0.1 end print(s) It will print 99.999999 instead of 100. To get around this, you can round, use a range of valid values, use ">=", or use integers (ie numbers without a decimal).

I propose using integers:

button = script.Parent
num = 50 --number of iterations to do in each loop; larger value means fading in/out will take longer
while true do
    for i = 1, num do
        button.BackgroundTransparency = 0.5+i/num/2
        wait()
    end
    for i = 1, num do
        button.BackgroundTransparency = 1 - i/num/2
        wait()
    end
end
0
Anyway yes, yours worked! :) thank you.. Bulvyte 388 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

Sorry to say, but you may need to read up more on the for loops here.

First, if you haven't realized, but your for loops are WAY off.

for i = 1, 0.25 do

Above is a big NO NO! Never try this, it will not work, the smaller number, .25, MUST come before the larger number, 1.

Since your code isn't very effective, I'm going to start from scratch. First, we'll declare the button variable!

button = script.Parent

Simple right? Next, we'll add the infinite loop in the script.

button = script.Parent

while(true) do
end

That was easy too, but let's get to the part you weren't sure of! From what I understand you're attempting to go from .5 transparency to 1, back to .5 and then repeat that, yes?

button = script.Parent

while(true) do
    if(button.BackgroundTransparency <= 0.5)
        -- Code to move to 1
    else
        -- Code to move to 0.5
    end
end

Above we added a function to see if the transparency is less than or equal to 0.5, and if so, we'll make it go to 1, else it will go down to 0.5!

button = script.Parent

while(true) do
    if(button.BackgroundTransparency <= 0.5)
        while(button.BackgroundTransparency < 1) do
            button.BackgroundTransparency = button.BackgroundTransparency + 0.02
            wait(0.1)
        end
    else
        while(button.BackgroundTransparency > 0.5) do
            button.BackgroundTransparency = button.BackgroundTransparency - 0.02
            wait(0.1)
        end
    end
end

This is the finished code. The while loops ensure that the transparency is what we want it to be! Hopefully this will work, but I can't ensure it because I've been programming in Java for the past month, so I may have messed up!

0
I get red marks on while this script is way confusing for me Bulvyte 388 — 9y
0
Line 4 is missing a "then". Also note that you don't need brackets around conditions in lua; ex, line 3 can be "while true do" chess123mate 5873 — 9y

Answer this question