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

Im trying to make a block fade in and out. Why is transparency going to 1.1 and -0.1?

Asked by 6 years ago

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.

01sphere = script.Parent
02transparency = 0
03fadeOut = true
04 
05while 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)
20end

2 answers

Log in to vote
0
Answered by
joeldes 201 Moderation Voter
6 years ago

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:

1sphere = script.Parent
2 
3local goal = {}
4goal.Transparency = 1   --Assuming that the original transparency is 0
5 
6game: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

Ad
Log in to vote
0
Answered by 6 years ago

There's a more efficient way to make it.

01sphere = script.Parent --- Make sure this is where the object you want to fade in and out transparency
02start = 0
03goal = 1
04----
05sphere.Transparency = start --- Ensure you start from transparency 0
06 
07while 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
14end

Answer this question