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.

sphere = script.Parent
transparency = 0
fadeOut = true

while true do
    if transparency <= 0 then
        fadeOut = true
    elseif transparency >= 1 then
        fadeOut = false
    end

    if fadeOut then
        transparency = transparency + 0.1
        sphere.Transparency = transparency
    else -- fadein
        transparency = transparency - 0.1
        sphere.Transparency = transparency
    end
    wait(1)
end

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:

sphere = script.Parent

local goal = {}
goal.Transparency = 1   --Assuming that the original transparency is 0

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

Ad
Log in to vote
0
Answered by 6 years ago

There's a more efficient way to make it.

sphere = script.Parent --- Make sure this is where the object you want to fade in and out transparency
start = 0
goal = 1
----
sphere.Transparency = start --- Ensure you start from transparency 0

while wait() do
    for i = 1,10 do
        sphere.Transparency = sphere.Transparency + 0.1
    end
    for i = 1,10 do
        sphere.Transparency = sphere.Transparency - 0.1
    end
end

Answer this question