As you can probably tell I’m not the best scripter and I find this fairly difficult to figure out, I’m trying to allow text to be able to slowly fade in, but I only currently found out how to make it fade out, and I’ve been working for over half and hour to 2 am in the morning and it’s getting ridiculous, here’s the script.
(Line 8 is the line where you insert the values, and currenly this part of the script only fades out, I want it to fade in!)
Is running for all values from 0 to 1, separated by 0.01 each time. It’s the same thing as
wait()
script.Parent.t2.TextTransparency=0
wait()
script.Parent.t2.TextTransparency=0.01
wait()
script.Parent.t2.TextTransparency=0.02
--- and repeat all the way up to 1
To go the other way, you have two choices:
Choice 1
Subtract i from 1, i.e. 1 - i. This would make it the equivalent of
wait()
script.Parent.t2.TextTransparency=1 - 0
wait()
script.Parent.t2.TextTransparency=1 - 0.01
wait()
script.Parent.t2.TextTransparency=1 - 0.02
--- and repeat all the way up to 1
Or, in other words:
wait()
script.Parent.t2.TextTransparency=1
wait()
script.Parent.t2.TextTransparency=0.09
wait()
script.Parent.t2.TextTransparency=0.08
--- and repeat all the way down to 0
Full example code:
for i = 0, 1, 0.01 do
wait()
script.Parent.t2.TextTransparency=1 - i
end
Choice 2
Make the loop go from 1 to 0, separated by -0.01. This is @EmeraldSlash’s example.
for i = 1, 0, -0.01 do
wait()
script.Parent.t2.TextTransparency=i
end
Which is the same as:
wait()
script.Parent.t2.TextTransparency=1
wait()
script.Parent.t2.TextTransparency=0.09
wait()
script.Parent.t2.TextTransparency=0.08
--- and repeat all the way down to 0
Unrelated note:
You can embed lua code using three backticks. That’s the key to the left of the “1” key on most keyboards. It looks like a weird apostrophe. `
I use a different method for fading in and out text, frames, etc. - it might not be convenient, but hey, at least it works!
-- To fade in.
repeat script.Parent.t2.TextTransparency = script.Parent.t2.TextTransparency - 0.1 wait(0.02) until script.Parent.t2.TextTransparency <= 0
-- To fade out.
repeat script.Parent.t2.TextTransparency = script.Parent.t2.TextTransparency + 0.1 wait(0.02) until script.Parent.t2.TextTransparency >= 1
I also figured that wait(0.02) is the best for fading objects as it behaves very smooth.
And to fade stuff slower, I subtract (or add if I’m fading out) 0.01 instead of 0.1 and change wait(0.02) to game:GetService("RunService").Heartbeat:Wait()
Some notes about both Andore’s and your implementations:
The method both you and Andore use can be slower if the FPS/Lua resume is slower than expected.
Your method can overshoot if the value is not at an exactly 1/10 value, which can happen due to floating point errors.
I can’t tell how long this will run for.
The smallest you can wait for is 1/30, or 0.0333..., but usually it ends up being more than that.
In testing wait times, wait(0.02) usually waited for times between 0.03 and 0.05 seconds.
The time it takes to fade in/out will always be longer than waitTime*iteration because wait waits for at least the time you give it, and the extra time adds up when you wait many times.
I can’t see any number for how long this will run, which would help maintainability and clarity.
There is a built-in way to do this: TweenService
Your implementation does not reset the value to 0 or 1 beforehand. I think that’s great! Just a small, but possibly significant difference if anyone uses your code.
Your implementation runs for a different amount of time depending on the current value of TextTransparency. Not better or worse, but possibly significant if anyone uses your code.
Fixed-time-length fading solves some of these problems:
Takes the same amount of time regardless of FPS
Takes the same amount of time regardless of initial value (unless otherwise specified)
Does not overshoot
Clearly shows how long it will run for
All of my examples will use a total run time of 2 seconds and fade to 1 transparency.
Lua example of fixed-time-length fading
local start = tick()
local timeLength = 2
local endValue = 1
local beginValue = script.Parent.t2.TextTransparency
local valueDifference = endValue - beginValue
while true do
if tick() >= start + timeLength then
break
end
script.Parent.t2.TextTransparency = beginValue + (tick() - start)/timeLength*valueDifference
wait() -- waits 1/30 by default. You can replace this with Heartbeat or RenderStepped
end
script.Parent.t2.TextTransparency = endValue
I don’t think this is much easier to maintain or read. It should be turned into a function:
local function tween(beginValue, endValue, timeLength, callback)
local valueDifference = endValue - beginValue
local startTime = tick()
while true do
local now = tick()
if now >= startTime + timeLength then
break
end
callback(beginValue + (now - startTime)/timeLength*valueDifference)
wait() -- waits 1/30 by default. You can replace this with Heartbeat or RenderStepped
end
callback(endValue)
end
-- if we reset to 0 first:
tween(0, 1, 2, function(i)
script.Parent.t2.TextTransparency = i
end)
-- if we start from current value:
tween(script.Parent.t2.TextTransparency, 1, 2, function(i)
script.Parent.t2.TextTransparency = i
end)
That was quite a bit of work! We can actually let Roblox handle all of this! This does the the same as that last example, while being shorter and clearer than all code provided so far.
game.TweenService:Create(script.Parent.t2, TweenInfo.new(2, Enum.EasingStyle.Linear), {TextTransparency = 1}):Play()
wait(2) -- wait for the tween to finish
In addition, with TweenService…
You can tween multiple properties at once
You have access to the built-in easing styles. This is normally set to Quad, but all of our other examples use Linear easing so I had to specify it in order to match our other examples.
You can repeat tweens automatically
You can have tweens reverse at the end automatically
You can tween after a delay
You can re-use tween infos and specify them once in an easy-to-configure place
You can tween from any current value to any passed-in value, you don’t have to assume it’s at 0 or 1 already.
Finally, I’d like to note that I didn’t include this in my original answer as it was somewhat out of scope and may have been an overwhelming amount of information for such a simple question. Since we’ve started discussion of alternate ways of doing this, I thought it would be helpful to mention arguable flaws in the original implementation and possibly more useful alternatives.
I haven’t thought of using TweenService for my work, but looking at it and how easy it is to code and the possibilities you could do with it. I see that it’s the best method imo. I might have to recode all my scripts that use my old inconvenient method
Also, just a tip - incrementing in decimal numbers can be buggy. I can’t remember the reason for this, it might be because of floating point rounding issues but you can often find that it doesn’t go up exactly to the number you specified. You might be better going from 100 to 0 and going down in increments of -1