Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Could someone explain how to use TweenService to me?

Asked by 5 years ago

So the last time I tried to make a smooth moving object (a rocket), I abused a for loop way too much, because I do not know how to use the TweenService. Could someone explain to me how to use it and maybe give an example?

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

So to use TweenService you need to put this in your game at the start of the script

1local tweenService = game:GetService("TweenService")

Then lets say you want to move a part, you'd need to give information about the tween (ex, how many times it will do this movement)

1TweenInfo.new(1,2,3,4,5,6)

in this example,

1 refers to how long this movement will take (in numbers)

2 refers to the style of the movement (though I don't fully understand this, you can just make it Enum.EasingStyle.Linear

3 refers to something that I don't understand either but you can just make it Enum.EasingDirection.InOut,

4 refers to the amount of times it will do the movement (in numbers)

5 refers to if it will reverse itself (true or false)

6 refers to the delay before the tween will happen (in numbers)

so you can do

1local tweeningInformation = TweenInfo.new (8, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)

Now that you have that information, you'll put where this part will go, you can do this

1local partProperties ={
2    Position = Vector3.new(x,y,z)}

Finally you mix this information together with

1local Tween = tweenService:Create(part,tweeningInformation,partProperties)

in the end your script should look something like this

1local tweenService = game:GetService("TweenService")
2local tweeningInformation = TweenInfo.new (8, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
3 
4local partProperties ={
5    Position = Vector3.new(141.787, 18.917, 489.98)}
6 
7 
8local Tween = tweenService:Create(part,tweeningInformation,partProperties)

and to run this tween you do Tween:Play() this answer may contain errors since this is the first time I've ever answered a question

0
nice avatar lol sean_thecoolman 189 — 5y
0
If anyone is wondering EasingDirection is whether or not the easing will appear before or after the part moves aGenericFoxDude 0 — 4y
Ad
Log in to vote
0
Answered by 5 years ago

TweenService is a one of those game services, so if you want to use it use the code "Game:GetService(TweenService)". Using the service itself is a little complex at first but once you get used to it, it'll get pretty handy. A tween is short for "inbetweening" because it can modify the animation between one position and another, or between whatever stuff you want. To use it, say TweenService:Create().

TweenService:Create accepts three variables. The first variable describes what object that you want to modify. For example, say this is script inside a brick and you want to modify that brick.

1local TS = Game:GetService("TweenService")
2local Part = script.Parent
3 
4local Tween = TS:Create(Part, nil, nil)

The second variable is a little complicated. It's describing what KIND of tweening you want it to do. It can be called by saying TweenInfo.new(). There are six statements inside this variable. The first is how long the Tween should last. The second is what kind of easing style the Tween should perform (eg should the animation slow down when it's coming to a stop, should the animation "bounce" a little, etc.) The third is which direction the tween will apply to (in or out), the fourth is how many times the tween should repeat, fifth is should it even repeat, and sixth is the delay in seconds between each tween. Below is an example if you're too lazy to read it.

01local TI = TweenInfo.new(
02 
035, --5 seconds NOTICE THE COMMA BETWEEN THE STATEMENTS
04Enum.EasingStyle.Bounce, --The EasingStyle is part of Enum so you need to call it from there
05Enum.EasingDirection.Out, --Same for EasingDirection
0610, --Repeating ten times
07true, --the tween will repeat
083 --3 second delay between the tweens
09 
10) --Don't forget to close your TweenInfo

The final variable inside the TweenCreate is what properties to tween. The variable itself is just an array of what properties to tween. You just need to state what property you want to modify, you don't need to do a root for it (ex. Part.Orientation, just do Orientation), but make sure that when you call the properties that you use the correct format for calling them (Size using Vector3, color using Color3/BrickColor, etc etc). Example if you're also too lazy lol.

1local TP = { --NOTICE THIS SPECIAL BRACKET DENOMINATING AN ARRAY
2 
3Orientation = CFrame.new(5,1,2); --SEMICOLON TO SEPARATE SINCE THIS IS AN ARRAY
4Size = Vector3.new(10,2,10);
5Color = Color3.fromRGB(127, 47, 0)
6} --DONT FORGET TO CLOSE IT

So after all of this nonsense and complex stuff, you can finally make your tween by compiling your three variables. Your part to modify, how you modify it, and what to modify. Additionally you need to do a :Play() on the tween, so don't forget that.

1Tween = TS:Create(Part, TI, TP)
2Tween:Play()

I'm sorry that this dang thing has to be so long, also if you're an expert in the field of tweening pleeeeassse correct me, but this is what I've learned so far. Additionally if this is too confusing there's a good video on this, link is here. Definitely check it out. https://www.youtube.com/watch?v=qgGDwTn0zgo Thanks!

0
i accept your answer bc i accepted the first one already sean_thecoolman 189 — 5y

Answer this question