Im new to scripting and am trying to create a sphere that continuously fades in and out. I have it working with this script although the transparency will go up to 1.1 and -0.1 and I cant figure out why. Heres what I have.
01 | sphere = script.Parent |
02 | transparency = 0 |
03 | fadeOut = true |
04 |
05 | while true do |
06 | if transparency < = 0 then |
07 | fadeOut = true |
08 | elseif transparency > = 1 then |
09 | fadeOut = false |
10 | end |
11 |
12 | if fadeOut then |
13 | transparency = transparency + 0.1 |
14 | sphere.Transparency = transparency |
15 | else -- fadein |
16 | transparency = transparency - 0.1 |
17 | sphere.Transparency = transparency |
18 | end |
19 | wait( 1 ) |
20 | end |
After looking at this, I am not sure of this reason either... I can make a guess that maybe it is because of floating point rounding error. Article
What I can tell you is that there is a better way of doing this. Consider using Tween's to do this:
Here is how you would do your fade thing:
1 | sphere = script.Parent |
2 |
3 | local goal = { } |
4 | goal.Transparency = 1 --Assuming that the original transparency is 0 |
5 |
6 | game:GetService( "TweenService" ):Create(sphere, TweenInfo.new( 1 , Enum.EasingStyle.Back, Enum.EasingDirection.In, - 1 , true ), goal) |
This should work tho I haven't tested it. I recommend you read this: Tween Service
There's a more efficient way to make it.
01 | sphere = script.Parent --- Make sure this is where the object you want to fade in and out transparency |
02 | start = 0 |
03 | goal = 1 |
04 | ---- |
05 | sphere.Transparency = start --- Ensure you start from transparency 0 |
06 |
07 | while wait() do |
08 | for i = 1 , 10 do |
09 | sphere.Transparency = sphere.Transparency + 0.1 |
10 | end |
11 | for i = 1 , 10 do |
12 | sphere.Transparency = sphere.Transparency - 0.1 |
13 | end |
14 | end |