How do you Tween a GUI Object forward and reverse infinitely?

So I am making a procedurally animated GUI Heart that grows and shrinks throughout the game’s lifetime. I am unsure how to make it grow and shrink infinitely and this is my current code:

function animate_heart()
    local info = TweenInfo.new(
        0.5,
        Enum.EasingStyle.Linear,
        Enum.EasingDirection.InOut,
        math.huge,
        true
    )

    local tween = game:GetService('TweenService'):Create(GUI, info, {Size = GUI.Size + UDim2.new(0, offset_X, 0, offset_Y)})
    tween:Play()
end

animate_heart()

Considering that math.huge is only the highest set number in Lua, the Tween will eventually stop. One alternative that I have tried to implement when making my infinite Tween is using RunService.RenderStepped, but that solution simply just doesn’t even work. This is the other variation of the code which doesn’t work:

function animate_heart()
    local info = TweenInfo.new(
        0.5,
        Enum.EasingStyle.Linear,
        Enum.EasingDirection.InOut,
        0,
        true
    )

    local tween = game:GetService('TweenService'):Create(GUI, info, {Size = GUI.Size + UDim2.new(0, offset_X, 0, offset_Y)})
    tween:Play()
end

game:GetService('RunService').RenderStepped:Connect(animate_heart)

The result of this code is that it continuously grows the GUI object without reversing to shrink it. One solution that I don’t want to implement in my code is this:

function animate_heart()
    local info = TweenInfo.new(
        0.5,
        Enum.EasingStyle.Linear,
        Enum.EasingDirection.InOut,
        0,
        true
    )

    local tween = game:GetService('TweenService'):Create(GUI, info, {Size = GUI.Size + UDim2.new(0, offset_X, 0, offset_Y)})
    tween:Play()

    tween.Completed:Wait()

    local tween = game:GetService('TweenService'):Create(GUI, info, {Size = GUI.Size - UDim2.new(0, offset_X, 0, offset_Y)})
    tween:Play()
end

game:GetService('RunService').RenderStepped:Connect(animate_heart)

I overall think that this is purely bad practice and don’t want to use code like this. Is there any solution to my problem?

You could still use the Tween but add a wait(TweenTime) to wait and play it again when it stops.

Edit: You would need a add a cooldown that works like a debounce.

Did you even fully read the question? This is not what I asked for.

function animate_heart()
    local info = TweenInfo.new(
        0.5,
        Enum.EasingStyle.Linear,
        Enum.EasingDirection.InOut,
        math.huge,
        -1,
        true
    )

    local tween = game:GetService('TweenService'):Create(GUI, info, {Size = GUI.Size + UDim2.new(0, offset_X, 0, offset_Y)})
    tween:Play()
end

animate_heart()

If the fifth value in TweenInfo.new() is below
zero, the tween will go on indefinitely.

1 Like

Looks like I need to read the ROBLOX wiki more for APIs.

2 Likes