Tween On Repeat, tell when finished

local BarTween = TweenService:Create(Bar, TweenInfo.new(0.8, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, -1), {
	Size = UDim2.new(0.906, 0, 0.644, 0),
})
local function PlaySync()
	BarTween:Play()
end

How do I tell if the repeating done finishes a loop each time?

You can use print() to know when itā€™s finished

The Completed event of the Tween will allow you to wait/perform further actions upon completion of the Tween.

Example:

Tween:Play()
Tween.Completed:Wait() -- Waits until the Tween finished before continuing
print("The tween has finished!")

If the Tween.Completed:Wait() was omitted, ā€œThe tween has finished!ā€ would print before the Tween has finished.

2 Likes

This wonā€™t do anything. I said RepeatCount is -1 meaning Tween.Completed wonā€™t work.

perhaps something like this could work. Iā€™m not at home to check if thereā€™s a better way but:

Tween:Play()
Tween.Completed:Connect(function()
     print("Do something")
     Tween:Play()
end)

it should run the code and print ā€œdo somethingā€ every time and ā€˜loopā€™ by just playing again after the completed event

thatā€™s if completed doesnā€™t run after a loop, not sure of that specific behavior

1 Like

Ah I was unaware of the implications that using -1 for had in relation to the Completed event (and I had originally thought that the Completed event would fire before the Tween replays, but I was mistaken).


Potential Solution

While I havenā€™t dealt with this kind of situation in the past, thereā€™s the potential that the ā€œmagnitudeā€ of the GuiObjectā€™s Size property could be utilized to determine when the Tween has gone back to its original position.

Take note that this is just a proof of concept and it could be further optimized.

local tweenLoopCount = 0 -- Amount of times the Tween has looped
local endOfTween = false -- "false" if the tweenLoopCount has not been increased on this current iteration, "true" if the opposite


local originalSize = UDim2.new(0.1, 0, 0.1, 0) -- The original Size of the GuiObject is stored here to compare with its current Size in order to determine the "magnitude"


while true do
	
	wait()
	
	local magnitudeCheck = (Item.Size.X.Scale - originalSize.X.Scale) -- Finds the difference between its current size on the X axis & its original size on the X axis. The Y Axis could be included, if needed

	
	if magnitudeCheck <= 0.4 and not endOfTween then -- If the X axis's size is less than 0.4 (provides some leeway due to the duration of the tween) and the tweenLoopCount has not been increased yet, then...
		
		endOfTween = true -- Adjusts the variable to true so the tweenLoopCount is not increased on the same iteration
		tweenLoopCount += 1
		warn("The tween has looped "..tweenLoopCount.." time(s)!")

	elseif magnitudeCheck >= 0.6 and endOfTween then -- If the X axis's size is greater than 0.6, we know that the Tween is almost complete...
		
		endOfTween = false -- Adjusts the variable back to false so the next iteration is able to have the tweenLoopCount increased
		print("The tween is restarting")
	end
end

Alternatively, instead of a ā€œmagnitudeā€ check, the GuiObjectā€™s current size could simply be compared to determine if it has exceeded a certain value rather than determining how much it has changed from its original size:

local currentSize = Item.Size.X.Scale

 -- Checks if its current size is less than or equal to 0.4
if currentSize <= 0.4 and not endOfTween then
    endOfTween = true
    tweenLoopCount += 1
    warn("The tween has looped "..tweenLoopCount.." time(s)!")

 -- Checks if its current size is greater than or equal to 0.6
elseif currentSize >= 0.6 and endOfTween then
    endOfTween = false
    print("The tween is restarting")
end

Hereā€™s a test place as a proof of concept:

Infinite Tween Proof of Concept.rbxl (24.6 KB)