Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years 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
6
Answered by
RayCurse 1518 Moderation Voter
6 years ago
Edited 6 years 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 = TweenInfo.new(3 , easeStyle , easeDirection)

--Create goal for properties
local goal = {
    Transparency = 1
    BackgroundColor3 = Color3.new(1 , 1 , 1)
}

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

--Play the tween
tween:Play()
0
Ths for all your help JakePlays_TV 97 — 6y
Ad
Log in to vote
0
Answered by 6 years 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)
end

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)
    end
    fading = false
end

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

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

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
fadeIn(myGui)
fadeOut(myGui)
-- then using specific fade times
fadeIn(myGui, 5)
fadeOut(myGui, 0.5)

Bonus:

-- 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
1
Tweening would be a much, much better way to do this. It's much smoother and uses 10x less lines of code. Eqicness 255 — 6y
0
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 966 — 6y

Answer this question