I have a function that creates a tween base, plays it, and returns the tween. When I call the function and use the .Completed
event, I’m getting different results depending on how I use it.
Using .Completed:Wait()
will have the proper behavior, it will yield until completed. On the other hand, using .Completed:Connect(function()
it will fire the completed event immediately without any yield. Does anyone know why?
local function CreateTween(Object, Info, Goal)
local Tween = TweenService:Create(Object, Info, Goal)
Tween:Play()
return Tween
end
Yields (desired):
CreateTween(Effect, TweenInfo.new(1), {TintColor = Color3.fromRGB(0, 0, 0)}).Completed:Wait()
Camera:RotateAroundPart(TargetParts[CurrentTarget], Radius, Speed, Offset)
CreateTween(Effect, TweenInfo.new(1), {TintColor = Color3.fromRGB(255, 255, 255)}).Completed:Wait()
Effect:Destroy()
Fires immediately:
CreateTween(Effect, TweenInfo.new(1), {TintColor = Color3.fromRGB(0, 0, 0)}).Completed:Connect(function()
Camera:RotateAroundPart(TargetParts[CurrentTarget], Radius, Speed, Offset)
end)
CreateTween(Effect, TweenInfo.new(1), {TintColor = Color3.fromRGB(255, 255, 255)}).Completed:Connect(function()
Effect:Destroy()
end)