Yeah! TweenService provides all of the tools you need to achieve this.
I made a little recreation of your scenario shown below:
Here is the code:
local TweenService = game:GetService("TweenService")
local part = script.Parent
local tweenInfo = TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.In, -1, true, 0)
local tweenObject = TweenService:Create(part, tweenInfo, {CFrame = part.CFrame * CFrame.new(0, 5, 0)})
tweenObject:Play()
As you can see, I use TweenService to create the animation. Here is a breakdown of the script.
Line 1:
Variable to store the TweenService
service
Line 2:
Variable to store the part to tween
Line 3:
Creation of a TweenInfo object. The TweenInfo object takes in the following arguments (in order):
- Animation time (in my case, 2 seconds)
- EasingStyle
- EasingDirection
- Number of times to repeat the animation (since I put -1, that would mean it would loop indefinitely)
- Reverses (I put true, meaning the animation plays in reverse after completing the initial tween)
- Delay time between animation
You can find more information about TweenInfo in this article.
Line 4:
Variable to store the TweenObject which is created from TweenService. TweenService:Create() takes the following arguments:
- Instance to tween (the part that I want to animate)
- A TweenInfo object (used the one I created on line 3)
- A table showing which properties of the instance to tween (I choose to change the CFrame property to the part’s current CFrame +5 in the y direction)
You can find more information about TweenService:Create() here.
Line 5:
Plays the created Tween.
Some more helpful articles:
https://developer.roblox.com/en-us/api-reference/class/Tween
https://developer.roblox.com/en-us/api-reference/class/TweenService
https://developer.roblox.com/en-us/api-reference/function/TweenBase/Play
https://developer.roblox.com/en-us/api-reference/enum/EasingStyle
https://developer.roblox.com/en-us/api-reference/enum/EasingDirection
@zilibobi 's approach should also work, but I suggest using TweenService for more concise and efficient code. Also, it’s always fun to take advantage of services that Roblox provides for us!
If you have any further questions, please ask!