How to loop tween

  1. What do you want to achieve? I want to loop these two tweens but I don’t know how.

  2. What is the issue? It only seems to play one tween and not the other.

  3. What solutions have you tried so far? I tried doing If statements but I can’t figure it out.

local button = script.Parent
local screen = script.Parent.Parent.Parent
local Player = game.Players.LocalPlayer
local StartingPosition = game.Workspace.StartingPosition
local SpawnLocations = {game.Workspace.StartingPosition, game.Workspace.StartingPosition1, game.Workspace.StartingPosition2, game.Workspace. StartingPosition3, game.Workspace.StartingPosition4}
local randomSpawn = SpawnLocations[math.random(1,#SpawnLocations)]
local background1 = button.Parent.Background
local background2 = button.Parent.Background2
local textlabel = button.Parent.TextLabel
local screenon = true

local Position = textlabel.Position


textlabel:TweenSizeAndPosition(
	UDim2.new(0.319,0,0.217,0), 
	UDim2.new(0.343,0,0.236,0), 
	"In",
	"Quad",
	1,
	false, 
	nil)
textlabel:TweenSizeAndPosition(
	UDim2.new(0.388,0,0.269,0),
	UDim2.new(0.302,0,0.206,0),
	"Out",
	"Quad",
	1,
	false,
	nil)

you should use TweenService for this purpose, TweenInfo has a Arument that allows you to repeat it for how many times you want, like this:

local info = TweenInfo.new(
    1, -- Duration
    Enum.EasingStyle.Quad, -- EasingStyle
    Enum.EasingDirection.InOut, -- EasingDirection
    1, -- Times Repeated
    false, -- reversed
    0 -- Delay
)

Which you can apply as such:

local TweenService = game:GetService"TweenService" -- Service

local info = TweenInfo.new( -- Tweeninfo
    1, -- Duration
    Enum.EasingStyle.Quad, -- EasingStyle
    Enum.EasingDirection.InOut, -- EasingDirection
    1, -- Times Repeated
    false, -- reversed
    0 -- Delay
)

TweenService:Create(Instance, info, Goal):Play() -- Creates Tween and Plays it

Instance is the Instance you want to Modify
info is the TweenInfo you are going to use
Goal is what property you want to change

For Goal, you can do this:

local Goal = {
	Size     = UDim2.fromScale(.319,.217), 
	Position = UDim2.fromScale(.343,.236), 
}

And when the Tween Repeats, you can have it reverse when it finishes, which you would set the Reserved Argument in Tweeninfo to true

1 Like

Thanks! But how would you then stop the tween once the ScreenGUI is disabled?

Tween:Cancel() should do the trick.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.