Help with reversing TweenService

Hey there, I’m trying to make a crate opening animation thing but ran into some trouble when trying reverse the reverse the value, not back to its original value but negtive.

Basically the base value is 0 and it goes to 30 then back down to 0 but I want it to go to negative 30 if that makes sense.

Any help is very much apricated! :slight_smile:

Script: (theres some code above that isnt relative by the way.)

local function Shake()
	local Speed = .4
	local tweenInfo = TweenInfo.new(
		Speed, -- Speed obviously
		Enum.EasingStyle.Linear, -- Style
		Enum.EasingDirection.InOut, -- Direction
		5, -- times repeated
		true, -- Reverse 
		0 -- Delay
	)
	local tween = TweenService:Create(part, tweenInfo, {Orientation = Vector3.new(0,0,30)}) -- 30 is the value I want to change to -30 after it reaches 30
	tween:Play()
end

wait(5)
Shake()
local TweenConn
local function Shake()
	local Speed = .4
	local tweenInfo = TweenInfo.new(
		Speed, -- Speed obviously
		Enum.EasingStyle.Linear, -- Style
		Enum.EasingDirection.InOut, -- Direction
		5, -- times repeated
		true, -- Reverse 
		0 -- Delay
	)
	local tween = TweenService:Create(part, tweenInfo, {Orientation = Vector3.new(0,0,30)}) -- 30 is the value I want to change to -30 after it reaches 30
	tween:Play() 
    TweenConn = tween.Completed:Connect(function()
         TweenConn:Disconnect()
         TweenConn = nil
         TweenService:Create(part, tweenInfo, {Orientation = Vector3.new(0,0,-30)}):Play()
    end
end

wait(5)
Shake()
1 Like

That just does it one dierction 5 times then the other dierction 5 time, sorry if I wasnt clear but I meant all in the same motion so like it goes 30 degrees then -30 degrees then repeats that 5 times, Sorry.

local function Shake()
	local Speed = .4
	local TimesToRotate = 5
	local tweenInfo = TweenInfo.new(
		Speed, -- Speed obviously
		Enum.EasingStyle.Linear, -- Style
		Enum.EasingDirection.InOut, -- Direction
		5, -- times repeated
		true, -- Reverse 
		0 -- Delay
	)
	local tween = nil
	for i = 1, TimesToRotate do
		tween = TweenService:Create(part, tweenInfo, {Orientation = Vector3.new(0,0,30)}) -- 30 is the value I want to change to -30 after it reaches 30
		tween:Play()
		tween.Completed:Wait()
		tween = TweenService:Create(part, tweenInfo, {Orientation = Vector3.new(0,0,-30)})
		tween.Completed:Wait()
	end
	tween = TweenService:Create(part, tweenInfo, {Orientation = Vector3.new(0,0,0)}) -- 30 is the value I want to change to -30 after it reaches 30
	tween.Completed:Wait()
end

The current code would rotate it by 30 degrees and would then rotate to -30 and I added a variable “TimesToRotate” which you can say how much times do you want it to rotate before going to it’s normal position 0.

Hope this helps you!

1 Like
-- Constants
local SPEED = .4
local REPEAT = 5 -- Times you want it to repeat
local Tweeninfo = TweenInfo.new(SPEED, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, true)

local function Shake()
   local Tween
   for _ = 1, REPEAT do
      Tween = TweenService:Create(part,  Tweeninfo, {Orientation = Vector3.new(0,0,30)})
      Tween:Play()
      Tween.Completed:Wait()
      Tween = TweenService:Create(part,  Tweeninfo, {Orientation = Vector3.new(0,0,-30)})
      Tween:Play()
      Tween.Completed:Wait()
   end
end

Edit: Pretty much what @HighFoxyplayz11 said

2 Likes

For some reason it’s still only rotating +30 degrees, I see what you did and i’m not really sure why it wont go -30. Thanks for the help :smiley:

1 Like

In the code change the 5 in the Tweeninfo to 0

1 Like

I might have forgotten to check that out <:D

1 Like

You mean this line?

		5, -- times repeated
1 Like

Yeah, since it’s already being repeated by the for loop there’s no need for it to repeat 5 times each iteration.

Yeah I tried changing it to 0 and all it did was just move it +30 once and then stop.

Which script are you using. If your using @HighFoxyplayz11 you need to add Tween:Play() after the completed

1 Like

Oh wait never mind, I tried your code and it worked fine :slight_smile:

1 Like

Just one more thing, If I wanna increase the speed each time so that the final time it does it the speed would be like .05 how would I go about doing that?

-- Constants
local SPEED = .4
local REPEAT = 5 -- Times you want it to repeat
local Tweeninfo = TweenInfo.new(SPEED, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, true)

local function Shake()
   local Tween
   for i = 1, REPEAT do
      if i == REPEAT then -- If the current Iteration is the last one
         Tweeninfo = TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, true)
      end
      Tween = TweenService:Create(part,  Tweeninfo, {Orientation = Vector3.new(0,0,30)})
      Tween:Play()
      Tween.Completed:Wait()
      Tween = TweenService:Create(part,  Tweeninfo, {Orientation = Vector3.new(0,0,-30)})
      Tween:Play()
      Tween.Completed:Wait()
   end
end

All you have to do is check what i is and if its the same as repeat it means its on the last run and in there you would simply change the tweeninfo to the new speed

1 Like

Sorry for the late reply. That kept the speed the same.

It should be working, what do you have your speed set to, and what is the speed when it’s the last iteration?

I got my speed set to 0.4 and I tried printing the speed everytime it completes and it always remains 0.4.

-- Constants
local REPEAT = 5 -- Times you want it to repeat

-- Var
local SPEED = .4

local function Shake()
   local Tween
   for i = 1, REPEAT do
      if i == REPEAT then -- If the current Iteration is the last one
         SPEED = 1
      end
      Tween = TweenService:Create(part,  TweenInfo.new(SPEED, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, true), {Orientation = Vector3.new(0,0,30)})
      Tween:Play()
      Tween.Completed:Wait()
      Tween = TweenService:Create(part,  TweenInfo.new(SPEED, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, true), {Orientation = Vector3.new(0,0,-30)})
      Tween:Play()
      Tween.Completed:Wait()
   end
end

Maybe something like this though I’m not sure why the other one does work.

1 Like

Well I got some progress, it stays .4 for 4 times then slows to 1 for the last one.