Tween.Completed not working

I want to tween a loading bar, which goes in reverse for a set size when a condition is met, and another tween is used to tween the loading bar as fully loaded.

So my problem is that the tween returns “Completed” even though I set it on pause to run the tween back in reverse?

if plr.Character:FindFirstChildOfClass("Tool").ID.Value == "Sliced" then
			tween:Play()

-- other code was not included
			if currentSize.X.Scale >= 0.1 then
				tween:Pause()
				reverseTween:Play()
				reverseTween.Completed:Wait()
				tween:Play()
			end
			
			tween.Completed:Connect(function()
				print("Completed")
			end)
		end
3 Likes

Is it possible that the function is being looped or there are multiple loops overwriting/cancelling the tween?

the function isnt being looped, its stored inside a proximity prompt. The only loops inside are two for loops to load in the possible dishes to a separate table, and a loop to which determines what image id to load in, it shouldn’t have any impact on the tween itself.

Perhaps the fact is that you pause the tween when it has already ended, or you incorrectly entered the conditions in the Tween.

Pleas, show your Tween function if it’s possible for you.

Here are the settings for my tween

local goal = {Size = UDim2.new(1,0,1,0)}
local part = loadingBar.Loading
local tweenInfo = TweenInfo.new(
	timer, -- Time
	Enum.EasingStyle.Linear, -- EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	0, -- RepeatCount 
	false, -- Reverses 
	0 -- DelayTime
)

local reverseTweenInfo = TweenInfo.new(
	timer / 2, 
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out, 
	0, -- RepeatCount 
	false, -- Reverses 
	0 -- DelayTime
)
local tween = tweenService:Create(part, tweenInfo, goal)

local currentSize = loadingBar.Loading.Size
local reverseGoal = {Size = UDim2.new(currentSize.X.Scale - 0.15, 0, 1, 0)}
local reverseTween = tweenService:Create(part, reverseTweenInfo, reverseGoal)

Oh also, the goal is to go from x = 0, to x = 1; the tween pauses if its bigger then 0.1, and then goes back in reverse when the if statement is met.

code 1

local part = loadingBar.Loading
local currentSize = loadingBar.Loading.Size -- this data is constant

local tween = tweenService:Create(part, TweenInfo.new(timer, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {
   Size = Udim2.fromScale(1, 1)
})

local reverseTween = tweenService:Create(part, TweenInfo.new(timer / 2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {
   Size = Udim2.fromScale(part.Size.X.Scale - .15, 1)
})

code 2

if plr.Character:FindFirstChildOfClass("Tool").ID.Value == "Sliced" then
			tween:Play()

-- other code was not included
			while part.Size.X.Scale < 0.1 do
                                task.wait(.02)
                        end
				tween:Pause()
				reverseTween:Play()
				reverseTween.Completed:Wait()
				tween:Play()
			
			tween.Completed:Connect(function()
				print("Completed")
			end)
		end

try to replace both

I replaced both, however, the reverse tween sets it back to its initial scale of 0 no matter what the size is; even when I put in an item into the pot as the first iteration, the reverse tween immediately plays, and “Completed” is printed out every time I put an item into the pot.

Are you reassigning the ‘tween’ variable anywhere in your proximity prompt function? Doing so appears to fire the ‘Completed’ event of the old tween, which may be the source of your issue.

I had a look and no, I havent reassigned the tween variable anywhere in my function, only the reverseTween variable inside my if statement in the code snippet above:

if plr.Character:FindFirstChildOfClass("Tool").ID.Value == "Sliced" then
			local reverseTween = tweenService:Create(part, TweenInfo.new(timer / 2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {
				Size = UDim2.fromScale(part.Size.X.Scale - 0.15, 1)
			})
			
			if currentSize.X.Scale == 0 then
				tween:Play()
				print("Playing Tween")
			end

			for itemName, indexes in pairs(foodList.food) do
				if plr.Character:FindFirstChild(indexes.slicedName) then -- if found in the index then set the current
					table.insert(storedItems, indexes.slicedName) -- add it to the current stored items
					currentID = indexes.slicedImageID
					local currentIndex = indexes.slicedName
					break
				end
			end

			local frameImage = currentTool.Handle.ImageBillboard:WaitForChild("Frame")["Image"..#storedItems]
			frameImage.Image = currentID
			plr.Character:FindFirstChildOfClass("Tool"):Destroy()
			loadingBar.Visible = true
			fire.Enabled = true
			water.Enabled = true

			if currentSize.X.Scale >= 0.1 then
				tween:Pause()
				reverseTween:Play()
				reverseTween.Completed:Wait()
				tween:Play()
			end

			tween.Completed:Connect(function()
				print("Completed")
			end)
		end

do not use CurrentSize, since this variable takes the value only 1 time and leaves it forever, try accessing the variable directly.

Odd, it seems like the tween is getting restarted somewhere because the time the tween takes to complete appears to increase as you add more items to the pot.

What you could try doing is storing the connection from ‘tween.Completed:Connect()’ and disconnecting it before assigning ‘tween’ or when entering the proximity prompt function if a connection was previously made. This should clean up any previous connections so only the latest is used when the tween finishes.

Also, you may want to only have this event connected right after the tween is assigned so using the proximity prompt doesn’t duplicate the connection and cause overlapping or redundancy.

check to make sure that the tween completed with the correct PlaybackState.
When a tween is paused or stopped it will fire completed and return a PlaybackState, or the reason it stopped. To get the PlaybackState simply add it to the connect part

tween.Completed:Connect(function(playbackstate)
	print("Completed", playbackstate)
end)

I modified the code so that the event is connected right after the tween is assigned, but it still did not change. I also added in the playbackState argument, and it seems that whenever I pause the current tween, and play another tween to tween it back to a smaller size, it returns with “Enum.PlaybackState.Cancelled”.

Is there any workaround to this?

Whenever you play a tween on an instance that is already “tweening” it will automatically cancel all other tweens currently playing on that instance. I guess you could check for the cancelled and then create a new one when the second tween finishes. Just make sure to calculate new time for the new tween

1 Like
tween.Completed:Connect(function(playbackstate)
	if tween.PlaybackState == Enum.PlaybackState.Completed then
		print("Completed")
	end
end)

Works, thank you very much!

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