Tween completed never firing?

My tween plays, but when I wait for it to finish, it never fires. The tween happens and the tween finishes, but when it does I never get the DONE printed

function Camera.Tween(tweenProperties, tweenInfo)
	CurrentCamera.CameraType = Enum.CameraType.Scriptable
	print("DOING TWEEN")
	local Info = tweenInfo or DefaultTweenInfo
	
	local TweenObject = TweenService:Create(
		CurrentCamera,
		Info,
		tweenProperties
	)
	TweenObject:Play()
	print("TWEENING", Info)
	return TweenObject
end

--// called from a function
local Tween = Camera.Tween({
	CFrame = Character.PrimaryPart.CFrame * CFrame.new(0, 2, 10),
	FieldOfView = 70
})
print("PLAYING")
coroutine.wrap(function()
	Tween.Completed:Wait()
	print("DONE") -- NEVER PRINTS
	CurrentCamera.CameraType = Enum.CameraType.Custom
end)()

I have also tried

if Tween.PlaybackState == Enum.PlaybackState.Playing then
	Tween.Completed:Wait()
end

But the print still doesn’t print

Calling coroutine.wrap() by itself will not run. You need to do this:

local f = coroutine.wrap(function()
	Tween.Completed:Wait()
	print("DONE") -- NEVER PRINTS
	CurrentCamera.CameraType = Enum.CameraType.Custom
end)

f()--call the coroutine

If you have any questions feel free to ask!

Are you sure there’s not some external issue causing this? I tested it with a part in workspace named Bob and this code and it worked fine for me (printed “Done”):

local TweenService = game:GetService("TweenService")

local function customTween(tweenProperties: {}, tweenInfo: TweenInfo): TweenObject
	local info = tweenInfo or TweenInfo.new(1)
	
	local tweenObject = TweenService:Create(
		workspace.Bob,
		info,
		tweenProperties
	)
	tweenObject:Play()
	
	return tweenObject
end

local tween = customTween({Position = workspace.Bob.Position + Vector3.new(0, 4, 0)})
tween.Completed:Wait()
print("Done")

What I have will work. Putting the double bracket after the end) automatically calls it

1 Like

I’m unsure what external thing could be causing problems tho. The print above it fires

Are you actually firing the tween?

I think it might be a problem with DefaultTweenInfo. Can you send the part of the code that defines it?

local DefaultTweenInfo = TweenInfo.new(0.5)

Yes, the tween is playing and it does finish, but when it finishes it stays locked in position and not following the character (cause its not on custom)

Have you tried getting rid of the coroutine?

It works then, but I need it to not yield (as in when I call this function, it needs to continue on without waiting for it to finish)

Perhaps you can try to use Tween.Completed:Connect?

local Tween = Camera.Tween({
	CFrame = Character.PrimaryPart.CFrame * CFrame.new(0, 2, 10),
	FieldOfView = 70
})

local CompletedConnection

CompletedConnection = Tween.Completed:Connect(function()
	print("DONE")
	CurrentCamera.CameraType = Enum.CameraType.Custom
	CompletedConnection:Disconnect()
end)

Is there a reason for all of these functions, really this should all be done in one function.

coroutine.wrap(function()
	Tween.Completed:Wait()
	print("DONE") -- NEVER PRINTS
	CurrentCamera.CameraType = Enum.CameraType.Custom
end)()

Since coroutine.wrap() just returns a function value which can be called to execute the created coroutine you need only append a pair of enclosing parentheses in order to call this returned function value.

function Camera.Tween(tweenProperties, tweenInfo)
	CurrentCamera.CameraType = Enum.CameraType.Scriptable
	print("DOING TWEEN")
	local Info = tweenInfo or DefaultTweenInfo

	local TweenObject = TweenService:Create(
		CurrentCamera,
		Info,
		tweenProperties
	)
	print("TWEENING", Info)
	return TweenObject
end

--// called from a function
local Tween = Camera.Tween({
	[CFrame] = Character.PrimaryPart.CFrame * CFrame.new(0, 2, 10),
	FieldOfView = 70
})
Tween:Play()
print("PLAYING")
coroutine.wrap(function()
	Tween.Completed:Wait()
	print("DONE") -- NEVER PRINTS
	CurrentCamera.CameraType = Enum.CameraType.Custom
end)()

I see, thank you for this info!