How can i reverse a TweenService Animation?

How can i reverse a TweenService Animation?

i want to achieve that my TweenService Animation reverse after 30 seconds
and if the Player Clicks the button Again the whole animation starts from new.

i tried so far looking on Youtube how other make ther script but i did not find a solution how to do it in my script because for some reason my Script looks completely different

Anyways here is my Script

local onCooldown = false

script.Parent.MouseClick:Connect(function(click)
	
	if onCooldown == false then

		onCooldown = true
		
	local TweenService = game:GetService("TweenService")

	local part = workspace.General.Button
	part.Position = Vector3.new(-3.417, 4.065, -15.827)
	part.Color = Color3.new(0, 1, 0)
	part.Anchored = true
	part.Parent = game.Workspace.General

	local goal = {}
	goal.Position = Vector3.new(-3.417, 4.001, -15.827)
	goal.Color = Color3.new(1, 0, 0)

	local tweenInfo = TweenInfo.new(1)

	local tween = TweenService:Create(part, tweenInfo, goal)

		tween:Play()
		
		task.delay(30, function()
			onCooldown = false
		end)
	end
end)

if anyone have a solution for the i would be very happy to hear it. and sorry if i did anything rong in this post.

2 Likes

Use delay in the TweenInfo and set it to 30 if that will help aswell as true for reverse boolean.

2 Likes

Sorry that maybe sounds dumb but how do i do like this?

local tweenInfo = TweenInfo.new(
			true,
			delay(30)
		)

because if i try this it gives me an error that delay needs 2 arguments.

1 Like

You need to set the full TweenInfo, see to this page: TweenInfo

1 Like

i tried it and i think i do every time something wrong could you give me and example how to use it?

1 Like
local tweenInfo = TweenInfo.new(
	2, -- Time
	Enum.EasingStyle.Linear, -- EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	-1, -- RepeatCount (when less than zero the tween will loop indefinitely)
	true, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)

Hope this helps!

1 Like

it works now but if i change delay time it doesn’t even Play the TweenService Animation should instead reverse after 30 seconds.

1 Like

Using the “reverse” parameter for TweenInfo won’t work because it reverses as soon as the tween finishes, regardless of DelayTime. What you have to do is make two tweens, one for tweening the part’s position and color to the goal, and then another for tweening it back to how it was.

local TweenService = game:GetService("TweenService")

	local part = workspace.General.Button
	part.Position = Vector3.new(-3.417, 4.065, -15.827)
	part.Color = Color3.new(0, 1, 0)
	part.Anchored = true
	part.Parent = game.Workspace.General

	local goal = {} --This is the goal for the first tween
	goal.Position = Vector3.new(-3.417, 4.001, -15.827)
	goal.Color = Color3.new(1, 0, 0)

    local reverseGoal = {} --This is the goal for the reverse tween
    reverseGoal.Position = part.Position
    reverseGoal.Color = part.Color

	local tweenInfo = TweenInfo.new(1)

	local tween = TweenService:Create(part, tweenInfo, goal) --This is the first tween

    local reverseTween = TweenService:Create(part, tweenInfo, reverseGoal) --This is the tween that reverses it back to normal

    tween:Play()

    wait(30) --Wait for 30 seconds

    reverseTween:Play() --Now play the tween that reverts it to how it was before
2 Likes

If you need it to reverse after 30 seconds then instead of using the “reverse” parameter of the instance method :Create() just create an additional tween which performs the reverse of the initial tween after thirty seconds have elapsed.

1 Like

this doesn’t even work at all.

1 Like

Okay i will try it later. :+1:

What exactly doesn’t work about it? It’s supposed to play the first tween, y’know, moving the part and shifting the color, then after 30 seconds, it plays another tween, which makes it move back to where it initially was and changes the color back to normal.

If it’s the reverse tween that’s not working, perhaps set the position and color of reverseGoal differently than how I did it.

local reverseGoal = {} --This is the goal for the reverse tween
reverseGoal.Position = Vector3.new(-3.417, 4.065, -15.827)
reverseGoal.Color = Color3.new(0, 1, 0)

it doesn’t even Play the Animation.

Maybe you aren’t playing the new animation?

I don’t really have any other ideas other than just creating a whole new tween.

i must say i did that before but in an other Script but some times this was pretty buggy.

You’re supposed to put this code under

script.Parent.MouseClick:Connect(function(click)
	
	if onCooldown == false then

		onCooldown = true

When it’s under this, it will play when the script’s parent is clicked.

Now it works but if i click the button again after 30 seconds it doesn’t work anymore.

local onCooldown = false

script.Parent.MouseClick:Connect(function(click)

	if onCooldown == false then

		onCooldown = true

local TweenService = game:GetService("TweenService")

local part = workspace.General.Button
part.Position = Vector3.new(-3.417, 4.065, -15.827)
part.Color = Color3.new(0, 1, 0)
part.Anchored = true
part.Parent = game.Workspace.General

local goal = {} --This is the goal for the first tween
goal.Position = Vector3.new(-3.417, 4.001, -15.827)
goal.Color = Color3.new(1, 0, 0)

local reverseGoal = {} --This is the goal for the reverse tween
reverseGoal.Position = part.Position
reverseGoal.Color = part.Color

local tweenInfo = TweenInfo.new(1)

local tween = TweenService:Create(part, tweenInfo, goal) --This is the first tween

local reverseTween = TweenService:Create(part, tweenInfo, reverseGoal) --This is the tween that reverses it back to normal

tween:Play()

wait(30) --Wait for 30 seconds

		reverseTween:Play() --Now play the tween that reverts it to how it was before
		
		task.delay(30, function()
			onCooldown = false
		end)
	end
end)

i apologize i did make a little mistake in the script i did now fix it the script looks like this

local onCooldown = false

script.Parent.MouseClick:Connect(function(click)

	if onCooldown == false then

		onCooldown = true

local TweenService = game:GetService("TweenService")

local part = workspace.General.Button
part.Position = Vector3.new(-3.417, 4.065, -15.827)
part.Color = Color3.new(0, 1, 0)
part.Anchored = true
part.Parent = game.Workspace.General

local goal = {} --This is the goal for the first tween
goal.Position = Vector3.new(-3.417, 4.001, -15.827)
goal.Color = Color3.new(1, 0, 0)

local reverseGoal = {} --This is the goal for the reverse tween
reverseGoal.Position = part.Position
reverseGoal.Color = part.Color

local tweenInfo = TweenInfo.new(1)

local tween = TweenService:Create(part, tweenInfo, goal) --This is the first tween

local reverseTween = TweenService:Create(part, tweenInfo, reverseGoal) --This is the tween that reverses it back to normal

		tween:Play()
		
		task.delay(30, function()
			onCooldown = false

wait(30) --Wait for 30 seconds

		reverseTween:Play() --Now play the tween that reverts it to how it was before
		end)
	end
end)

and it works perfectly fine.

Thank you so much for all the help!