How would I tween transparency?

Soo I am trying to make like when you touch a brick a gui just tween’s it’s transparency but I really don’t know how to do that, and I tried searching it and some code that gets that task done but it looks very bad, code:

script.Parent.TextLabel.Transparency = 0.1
wait(0.1)
script.Parent.TextLabel.Transparency = 0.2
wait(0.1)
script.Parent.TextLabel.Transparency = 0.3
wait(0.1)
script.Parent.TextLabel.Transparency = 0.4
wait(0.1)
script.Parent.TextLabel.Transparency = 0.5
wait(0.1)
script.Parent.TextLabel.Transparency = 0.6
wait(0.1)
script.Parent.TextLabel.Transparency = 0.7
wait(0.1)
script.Parent.TextLabel.Transparency = 0.8
wait(0.1)
script.Parent.TextLabel.Transparency = 0.9
wait(0.1)
script.Parent.TextLabel.Transparency = 1
wait(0.1)
print("Finished")
21 Likes

This could be`so simply replaced with

for i = 0, 1, 0.1 do
     wait(0.1)
     script.Parent.TextLabel.Transparency = i
end
print("Finished")

Always make sure to use cleaner and more efficient code, never do that multiple line. This is exactly what a for loop is for.

Or if you’re looking for tweening then just a for loop do

local TweenService = game:GetService("TweenService")
local time = 1 --this will tell you how much it would take for the tween to finish
local tween = TweenService:Create(script.Parent.TextLabel, TweenInfo.new(time), {Transparency = 1})

tween:Play()

More info

33 Likes

its really simple, using TweenService:Create you can do this code:


local Info = TweenInfo.new(1)
local Tween = game:GetService("TweenService"):Create(script.Parent,Info,{TextTransparency=1})
Tween:Play()

print("Finished")

or use the script above

Tweens are smother than for loops

57 Likes

This is how you would go by it.

local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(
    1, --Time
    Enum.EasingStyle.Linear, --EasingStyle
    Enum.EasingDirection.Out, --EasingDirection
    0, --Repeat count
    false, --Reverses if true
    0 --Delay time
)
local tween = tweenService:Create(part, tweenInfo, {Transparency = 1})
tween:Play()

Replace “part” with the part you want to tween.

26 Likes
local step = 0.1 --smaller step = smoother animation
repeat
script.Parent.TextLabel.Transparency = part.Transparency - step
game:GetService("RunService").Heartbeat:Wait()
until script.Parent.TextLabel.Transparency == 1

Don’t use RenderStepped as a substitute for loop intervals ever. RenderStepped should be scarcely used in your code as it executes before frames and thus has the potential to delay or block rendering if you’re using it inappropriately. RenderStepped realistically should only be used for small immediate updates that should be present before any rendering, such as camera and character updates. Use Heartbeat here instead. If it’s important transparency comes before physics are simulated, use Stepped.

18 Likes

I’ve tried the loop code, it works but only makes the part fade out and never fade back in. How could I make it neverending to have the part have full transparency to opaque then back to full transparency and so on?

1 Like

You simply make a separate tween, where the transparency is set back to 0, play it as well, do that along with the other code in a while loop. You should do that on your own, but here is a version of what I said.

local TweenService = game:GetService("TweenService")
local time = 1 --this will tell you how much it would take for the tween to finish

local FadeIn = TweenService:Create(script.Parent.TextLabel, TweenInfo.new(time), {Transparency = 1})
local FadeOut = TweenService:Create(script.Parent.TextLabel, TweenInfo.new(time), {Transparency = 0})

while true do
    FadeIn:Play()
    FadeIn.Completed:Wait() --this will yield/stop the script until FadeIn tweem has finished
    FadeOut:Play()
    FadeOut.Completed:Wait() -- stops until FadeOut is done, google :Wait for more info
end
10 Likes

This helpes so much. Sometimes the easiest approach is the best. Thanks!

2 Likes

i know that use tweenservice is easier way, but if you want to use your first way, you better do this with loop:

for i=100, 1, -1 do
    script.Parent.TextLabel.Transparency = i/100
end

it’s smoother than making 0.1 more transparent (beacuse it’s 0.01) and to make it you just need to use 3 lines. =) (instead of thousands)

For some cases, TweenService is better, as TweenService does not yield the current thread, which is useful if you need to do multiple things at the same time whilst a tween or animation is running. You can also do it in one line

2 Likes

I know, that tween service is much better and easier, i already said it.
And by saying “if you need to do multiple things at the same time whilst a tween or animation is running” you mean, that roblox will wait for function (for i=100, 1, -1 do) before doing next lines? If yes we can just use task.spawn function

task.spawn(function()
    for i=100, 1, -1 do
        script.Parent.TextLabel.Transparency = i/100
    end
    --or other, everything we want
end)

it’s will make roblox don’t wait for function’s ending
also sorry for mistakes, i don’t know english very well.

1 Like