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