GUI Animations

Basic Tweening, Additional Options, Advanced Tweening And More!

by I_Yagyaveer

Author Avatar

GUI Animations

In animation, tweening is an abbreviation for “in-betweening,” the process of generating intermediate frames between two key points in a sequence. When designing a user interface, tweening can be used to transition a GUI smoothly from one position/state to another, for instance:

Basic Tweening

These and other GUIs like Frame and ScrollingFrame can all be tweened via a script attached directly to the object.

  1. In the Explorer window, hover over the StarterGui object, click on the circle +  button, and insert a ScreenGui object.
    
  2. Select the new ScreenGui object and, in a similar manner, insert a TextButton.
    
  3. Select the new TextButton and insert a new **LocalScript**.
    

img|85x33

4.Copy and paste the following code into the script:

local object = script.Parent
object.AnchorPoint = Vector2.new(0.5, 0.5)

With these variables in place, you can now tween the GUI with built-in methods of its base GuiObject class.

Position

The GUI position can be tweened using the GuiObject:TweenPosition() method. In its most basic form, this method requires an end position (UDim2) for the object’s destination:

local object = script.Parent
object.AnchorPoint = Vector2.new(0.5, 0.5)
object.Position = UDim2.new(0.1, 0, 0.5, 0)

wait(2)

object:TweenPosition(UDim2.new(0.5, 0, 0.5, 0))

img|25x10

Note that this tween uses the scale parameters of UDim2 versus the positional offset values. This ensures that the GUI tweens to the center of the screen, no matter the screen's size or orientation.

Alternatively, you can make the GUI start off the left edge of the screen by offsetting the object’s starting position by its width:

local object = script.Parent
object.AnchorPoint = Vector2.new(0.5, 0.5)
object.Position = UDim2.new(0, -object.Size.X.Offset, 0.5, 0)

wait(2)

object:TweenPosition(UDim2.new(0.5, 0, 0.5, 0))

Size

A GUI’s size can be tweened using the GuiObject:TweenSize() method which accepts a UDim2 for the object’s final size:

img|25x10

The above example uses the scale parameters of UDim2, but you can also resize the GUI using explicit size parameters:

local object = script.Parent
object.AnchorPoint = Vector2.new(0.5, 0.5)
object.Position = UDim2.new(0.5, 0, 0.5, 0)

wait(2)

object:TweenSize(UDim2.new(0, 400, 0, 100))

Position and Size

To tween both the position and size of a GUI in one command, use the GuiObject:TweenSizeAndPosition() method. This requires a UDim2 for both the final size and position of the GUI:

local object = script.Parent
object.AnchorPoint = Vector2.new(0.5, 0.5)
object.Position = UDim2.new(0, -object.Size.X.Offset, 0.5, 0)

wait(2)

object:TweenSizeAndPosition(UDim2.new(0.4, 0, 0.4, 0), UDim2.new(0.5, 0, 0.5, 0))

Additional Options

All of the above methods accept additional options which can fine-tune or provide more control over GUI animations.

Animation Easing Animation easing defines a “direction” and style in which the tween will occur.

Direction:-

Style:-

Easing options can be defined as EasingDirection and EasingStyle enums after the size and/or position UDim2 values:

local object = script.Parent
object.AnchorPoint = Vector2.new(0.5, 0.5)
object.Position = UDim2.new(0, 0, 0.5, 0)

wait(2)

object:TweenPosition(UDim2.new(0.5, 0, 0.5, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quint)

Time

By default, a tween will occur in 1 second, but this can be adjusted by specifying a number of seconds following the easing options:

local object = script.Parent
object.AnchorPoint = Vector2.new(0.5, 0.5)
object.Position = UDim2.new(0, 0, 0.5, 0)

wait(2)

object:TweenPosition(UDim2.new(0.5, 0, 0.5, 0), nil, nil, 3)

Note that nil can be used for either of the easing options if you don't need to change the default EasingDirection of Out or the default EasingStyle of Quad.

Advanced Tweening

The methods above are simple ways to tween a GUI’s position and size, but they cannot tween other aspects like rotation, color, or transparency. For these, you’ll need to use TweenService, a powerful tool which can tween almost any property or combination of properties.

Color Tween Some animations require a tween of a Color3 property, for instance changing a health bar’s color from green to yellow if a player’s health falls below a certain percentage.

  1. In the Explorer window, hover over the ScreenGui object you created earlier and insert a Frame.
    
  2. Select the new frame object and insert a new LocalScript.
    
  3. Copy and paste the following code into the script:
    
local TweenService = game:GetService("TweenService")

local frame = script.Parent
frame.Position = UDim2.new(0, 20, 0, 20)
frame.BorderSizePixel = 0
frame.Size = UDim2.new(0, 400, 0, 30)
frame.BackgroundColor3 = Color3.fromRGB(0, 255, 75)

-- Declare target size and color values
local newSize = UDim2.new(0, frame.Size.X.Offset*0.5, 0, frame.Size.Y.Offset)
local newColor = Color3.fromRGB(255, 255, 50)

-- Set up tween
local tweenInfo = TweenInfo.new(1.5, Enum.EasingStyle.Quart, Enum.EasingDirection.Out)
local tween = TweenService:Create(frame, tweenInfo, {Size=newSize, BackgroundColor3=newColor})

wait(2)

tween:Play()

This code does the following:

Sequential Tween

Some animations may benefit from a sequential setup, for example a rotation tween following a movement tween. This can be done by starting the second tween following the first tween’s Completed event:

local TweenService = game:GetService("TweenService")

local frame = script.Parent
frame.AnchorPoint = Vector2.new(0.5, 0.5)
frame.Position = UDim2.new(0, 70, 0, 70)
frame.BorderSizePixel = 0
frame.Size = UDim2.new(0, 100, 0, 100)
frame.BackgroundColor3 = Color3.new(0, 0, 0)

-- Set up tweens
local tweenInfo1 = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
local tween1 = TweenService:Create(frame, tweenInfo1, {Position=UDim2.new(0.5, 0, 0.5, 0)})
local tweenInfo2 = TweenInfo.new(1.5, Enum.EasingStyle.Quart, Enum.EasingDirection.InOut)
local tween2 = TweenService:Create(frame, tweenInfo2, {Rotation=180})

tween1.Completed:Connect(function()
    tween2:Play()
end)

wait(2)

tween1:Play()

View in-game to comment, award, and more!