Reversing a Tween

:wave:t4: Hello, I was wondering if there is a way to reverse a tween without setting the tweenInfo to true? If not, I’ll just duplicate the code and set the values back to normal. Looking for a cleaner/less code way of completing this.

example

local Effect = game:GetService("TweenService"):Create(ColorCorrection, tweenInfo, goals)
Effect:Play()	
Effect.Completed:Wait()
-- Do other stuff, not reversing it immediately 
Effect:Reverse() -- After the effect finishes playing, reverse what it did

Is there a way to have this “:Reverse()” play the same tween backwards?

I appreciate any help, thanks! :pray:t4:

2 Likes

An idea that comes to my mind is you can toggle the reverse in TweenInfo BUT you pause the tween after it finished going to it’s goal, then run your other code before unpausing the tween.

2 Likes

I’ll give that a try, thanks for the reply.

There really isn’t a really good way to reverse a tween other than to create a new tween entirely. If you want to make a readable and more cleaner approach, then you can do this:

local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(10)

local function reversedTween(object, info, goals, toCall)
	local previousGoals = {}
	for property, _ in pairs(goals) do
		previousGoals[property] = object[property]
	end
		
	local listener = tweenService:Create(object, info, goals)
	listener:Play()
	listener.Completed:Wait()
	
	if toCall then toCall() end
	tweenService:Create(object, info, previousGoals):Play()
end

local function randomFunction()
	print("This is printed after the tween.")
end

reversedTween(workspace.RandomPart, tweenInfo, {Position = Vector3.new(0, 100, 0)}, randomFunction)

Essentially, we’re just creating logging the properties we’re going to change, tweening the object, calling a given function, and reversing the tween with a new one that using the properties we previously logged. It may not be a great approach in terms of preformance, but I wouldn’t worry too much about that and memory usage unless it becomes a visible issue.

As a side note, the last function parameter is the function that is called after the initial tween in completed. It can be left empty if you wish.

4 Likes