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!
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
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!