Tween.Completed Not working

Tween.Completed:Wait() seems to yield for a long time, even past the tween actually visually finishing and it seems to maybe never end, why is this?

The print to where the red arrow is pointing at doesn’t actually print, so it seems there’s a yield happening with Tween.Completed:Wait()

You need to use Tween:Play() below the Tween variable. (Since the Play function doesn’t returns it) It would look like this:

local Tween = TweenService:Create(...)
Tween:Play()
Tween.Completed:Wait()
1 Like

Wait yields the current thread until the given time has elapsed. I’d recommend using this, it may work better:

for i = 1, MaxVal do
    Camera.CameraType = Enum.CameraType.Scriptable
    local TweenHere = TweenService:Create(Camera, TweenInfo.new(CurrentTime, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false, 0), {CFrame = TotalCamera{i + 1}.CFrame}):Play()

    TweenHere.Completed:Connect(function()
      print("Status: Tween Completed!")
   end
end

Side note: It is recommended to post the actual code here, and not the image of it!

As @MeCrunchAstroX said, Tween:Play() doesn’t return the Tween. In other words, it’s not really ‘chainable’.

Besides that, I really recommend you create a wrapper library for tweening functions as it can make playing tweens & being able to get it from that really useful, especially for animated interfaces!

Here’s the one I use as an example:

local TweenService = game:GetService("TweenService")
local Tween = {}

function Tween:Create(Instance: Instance, tweenInfo, Properties): Tween
	if typeof(tweenInfo) == "table" then
		if tweenInfo[2] and typeof(tweenInfo[2]) == "string" then
			tweenInfo[2] = Enum.EasingStyle[tweenInfo[2]]
		end
		if tweenInfo[3] and typeof(tweenInfo[3]) == "string" then
			tweenInfo[3] = Enum.EasingDirection[tweenInfo[3]]
		end
		tweenInfo = TweenInfo.new(unpack(tweenInfo))
	end
        
	return TweenService:Create(Instance, tweenInfo, Properties)
end

function Tween:Play(Instance: Instance, tweenInfo, Properties): Tween
	local NewTween = self:Create(Instance, tweenInfo, Properties)
	NewTween:Play()
	
	return NewTween
end

return Tween

Usage:

local Tween = require(path.to.Tween)

local NewTween = Tween:Play(Gui.Frame, {1, "Sine", "Out"}, {Transparency = 1}) -- Automatically runs Tween.Play and returns Tween
if NewTween.PlaybackState == Enum.PlaybackState.Playing then
    NewTween.Completed:Wait()
end
5 Likes