New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Still have questions? Join our Discord server and get real time help.
Log in to vote

Is there a way i can do a function for Gui fading?? Need help with a gui?

Asked by 1 year ago

Is there a service like TweenPosition but FadeGui that I can do to fade a solid color gui into the screen? Ive tried to reseach, I cant seem to find anything. I want it to be like you see a npc that looks llike you. then it fades in a gui, says something, and then fades out. I needed to say this extra cause the server said i needed to describe my problem, just need help on a fade funtion xD

2 answers

Log in to vote
Answered by
RayCurse 1363 Moderation Voter
1 year ago
Edited 1 year ago

Roblox has a service called TweenService which can be used for what you are asking. In fact, it can be used to tween almost any visual property such as transparency, position, and color. In order to use tween service, you first need to instantiate a TweenInfo object. Then you can pass this info into the Create() function in TweenService to get a TweenObject.

local gui = screenGui.Frame --example

--Create tween info
local easeStyle = Enum.EasingStyle.Quint
local easeDirection = Enum.EasingDirection.In
local info = , easeStyle , easeDirection)

--Create goal for properties
local goal = {
    Transparency = 1
    BackgroundColor3 = , 1 , 1)

--Create tween object
local TweenService = game:GetService("TweenService")
local tween = TweenService:Create(gui , info , goal)

--Play the tween
Ths for all your help JakePlays_TV 97 — 1y
Log in to vote
Answered by 1 year ago

I actually developed this just the other day!

This needs to be in a LocalScript

local DEFAULT_DURATION = 2 -- seconds
local RunService = game:GetService("RunService")
local fading

local function lerp(a, b, t)
    return a * (1-t) + (b*t)

local function fade(gui, startTransparency, endTransparency, duration)
    fading = true
    duration = duration or DEFAULT_DURATION
    local elapsed = 0
    while elapsed < duration do
        elapsed = elapsed + (RunService.Heartbeat:Wait())
        gui.BackgroundTransparency = lerp(startTransparency, endTransparency, elapsed/duration)
    fading = false

local function fadeIn(gui, duration)
    while fading do wait() end
    gui.Visible = true
    fade(gui, 1, 0, duration)

local function fadeOut(gui, duration)
    while fading do wait() end
    fade(gui, 0, 1, duration)
    gui.Visible = false

Example usage, appended at the bottom of the above script:

-- this example fades MyGui in and out twice
local myGui = game.Players.LocalPLayer.PlayerGui.ScreenGui.MyGui
-- first using the default fade time of 2 seconds
-- then using specific fade times
fadeIn(myGui, 5)
fadeOut(myGui, 0.5)


-- this example makes fading accessable via BindableFunctions
local myGui = game.Players.LocalPLayer.PlayerGui.ScreenGui.MyGui
local fadeInBindable = myGui.FadeInBindableFunction
local fadeOutBindable = myGui.FadeOutBindableFunction
fadeInBindable.OnInvoke = fadeIn
fadeOutBindable.OnInvoke = fadeOut
Tweening would be a much, much better way to do this. It's much smoother and uses 10x less lines of code. Eqicness 257 — 1y
Yes, I agree, and I upvoted the above answer. This does, however, show another approach and gives some insight on how to use the RunService Heartbeat with respect to real time. WillieTehWierdo200 897 — 1y

Answer this question