What do you want to achieve? Keep it simple and clear!
Want to make this kind of Animation:
What is the issue? Include screenshots / videos if possible!
I want my Text label to make a rotation loop, but it only goes one time and doesn’t do it again as you can see in the attached video:
What solutions have you tried so far?
I searched how to fix it on DevForum but I didn’t find anything. This my code:
local Title = script.Parent
local Rotation = Title.Rotation
for i = -10, 10 do
Title.Rotation = i
wait(0.1)
end
The main thing to know about tweens is that there is a thing called easing styles & directions. A great site for this is https://easings.net/ - hover over the different graphs and see how the speed at which a property is adjusted changes over time. This can be useful to make different types of animations.
Here’s an example of how to use this:
-- Title is the variable which references the object you want to move. Make sure to set it.
local TS = game:GetService("TweenService");
local Time = 1;
local Info = TweenInfo.new(
Time,
Enum.EasingStyle.Sine, -- Uses sine wave easing style
Enum.EasingDirection.InOut -- Smoothly start and stop rotating. Check listed site for details
);
while true do
TS:Create(Title, Info, { Rotation = 10 }):Play();
task.wait(Time);
TS:Create(Title, Info, { Rotation = -10 }):Play();
task.wait(Time);
end
Your code does not work since it is in a for loop, but it is not wrapped in a while loop/repeat; which in your case, it will only start rotation from -10, and increment to 10.
You could although wrap this in a while loop, but it will snap to where it started; instead of rotating back to original position in an animation.
Imo the more optimized way to do instead of while true do is to check when tween completed and then play second tween. Like 1st tween rotates on 20 and 2nd tween rotates to -20 and you basically repeat it by tween.Completed:Connect(function()
I know this has already been marked as solved, but I wanted to throw out into the open this easier solution. You can easily produce a fluid looking rotation loop by using RunService.Hearbeat and math.sin combined with tick (you can use deltaTime provided by .Heartbeat, but tick is easier to understand).
local DEGREES_OF_ROTATION = 20
local SPEED = 2
local textLabel = script.Parent
game:GetService("RunService").Heartbeat:Connect(function()
textLabel.Rotation = math.sin(tick()*SPEED) * DEGREES_OF_ROTATION
end)
Doing this will land you this result (ignore the choppy gif loop):
Seems my edit didn’t go through before, but yes the ‘TweenBase’ objects would need to be assigned to variables in order for the Wait instance method to be called through/on their Completed RBXScriptSignal objects.
After further inspection the proposed solution leaves a lot to be desired.
local TweenService = game:GetService("TweenService")
local Title = nil --Define 'Title' here.
Title.Rotation = -10
local Tween = TweenService:Create(Title, TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, -1, true), {Rotation = 10})
Tween:Play()
Here we utilise TweenService:Create's ‘repeatCount’ and ‘reverses’ parameters in order to create a single ‘TweenBase’ object which loops indefinitely.