Make a part that can tween up and down

I’m trying to make a part that can move up and down infinitely using tween service however there’s lots of lag and when I try to call two tween animations the part get stucked to the same position for ever

Here’s the outcome

local function translateDownFunc()
	local translationSpeed = 3

	local tweenInfoTranslate = TweenInfo.new(translationSpeed, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
	
	local goalTranslation = {Position = defaultPartPosition + Vector3.new(0, 3, 0)}
	local goalTranslation2 = {Position = defaultPartPosition + Vector3.new(0, -3, 0)}

	local translatePart = TweenService:Create(part, tweenInfoTranslate, goalTranslation)
	local translatePart2 = TweenService:Create(part, tweenInfoTranslate, goalTranslation2)
	
	translatePart:Play()
	translatePart2:Play()
	
	translateDownFunc()
end

translateDownFunc()

You’re playing both the tweens at the same time. To fix this, do something like this

local translationSpeed = 3

local tweenInfoTranslate = TweenInfo.new(translationSpeed, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
	
local goalTranslation = {Position = defaultPartPosition + Vector3.new(0, 3, 0)}
local goalTranslation2 = {Position = defaultPartPosition + Vector3.new(0, -3, 0)}

local translatePart = TweenService:Create(part, tweenInfoTranslate, goalTranslation)
local translatePart2 = TweenService:Create(part, tweenInfoTranslate, goalTranslation2)

function down()
translatePart2:Play()
translatePart2.Completed:Connect(function()
up()
end)
end
function up()
translatePart:Play()
translatePart.Completed:Connect(function()
down()
end)
end

up()

im not sure why ur doing this

in the function down() ur already calling up() but it doesn’t know that up() exists yet since u put it above the function

you can simply do this:

translatePart:Play()
translatePart.Completed.Wait()
translate.Part2:Play()

you can also make this into a loop

I litterally tried this first and didn’t work I’ll tried the other one code

Yayt your code doesn’t work either I don’t see part moving at all

did you check the output? are there any errors? also i accidentally made a typo in my solution so try changing that

By the way you put a . it’s translatePart2

i know, have you tried removing it?

Yeah and there’s also wait . it’s :

i was half awake typing this so try fixing all the typos i made and see

It work at the start but doesn’t repeat itself infinitely and sometimes the part instantly move to the y position and the animation isn’t smooth at all

Do you want me to add you in the game so you have a better grasp about the overall code ?

Alright here’s the video

Wait a minute I’m close to finding solution

If you want something like this, use :lerp
robloxapp-20240311-1039476.wmv (1.4 MB)

How would I use it in my own tweening and I see that you know how to make pets and eggs system that’s cool ?

I tried this line of code but doesn’t seems to work well

function up()
	--translatePart:Play()
	script.Parent.CFrame = script.Parent.CFrame:Lerp(script.Parent.CFrame + CFrame.new(0, 3, 0), 1)
	wait(3)
	script.Parent.CFrame = script.Parent.CFrame:Lerp(script.Parent.CFrame + CFrame.new(0, -6, 0), 1)
	wait(3)
	script.Parent.CFrame = script.Parent.CFrame:Lerp(script.Parent.CFrame + CFrame.new(0, 3, 0), 1)
	wait(3)
	up()
	--translatePart.Completed:Wait()
	--defaultPartPosition += Vector3.new(0, 3, 0)
	--translatePart2:Play()
	--defaultPartPosition += Vector3.new(0, -3, 0)
	--translatePart2.Completed:Connect(up)
end

up()

That’s because it’s not in a loop. Try adding it inside a while function

How did you use Lerp concept to achieve your things on the pet system I don’t understand how to use lerp

What I’m trying to achieve basically is the part slowly moves at the start then it increases exponentially until the end goal but to achieve this with :Lerp() function which stands for Linear Interpolation I would basically have to change the interpolation value so like basically if I want to change the speed says at the start I want 0.2 and at the end 0.8 how do I achieve this ?

Alright :Lerp seems to be working fine but parts are teleporting and I have no idea why it does this annoying issue

local part = script.Parent

local lowerBound = part.Position - Vector3.new(0, 0.29, 0)
local upperBound = part.Position + Vector3.new(0, 0.25, 0)

local radiansPerSecond = math.rad(90) -- a quarter turn every second (we need to do this to maintain a consistent velocity), needs to be wrapped in math.rad as CFrame.Angles takes an arg in radians not degrees

local identityRotation = CFrame.identity.Rotation -- equivalent of CFrame.Angles(0, 0, 0)

local currentRotation = identityRotation -- our starting rotation
local isUp = true
--------------------------------------------
--										  --
--					UP/DOWN				  --
--										  --
--------------------------------------------
while true do

	local now = tick()
	wait()

	if isUp == true then

		
		for i = 1, 25, 0.1 do
			-- math.sin returns a value between -1 and 1 so we first need to add 1 to get a number between 0 and 2, then divide by 2 (multiply by 0.5) to get a number between 0 and 1 so it does not exceed the boundaries we set above
			local position = upperBound:Lerp(lowerBound, (math.sin(i)) * 1)

			-- finally apply our current CFrame to the part's CFrame
			part.CFrame = CFrame.new(position) 

			wait()
		end
		
		isUp = false
	elseif isUp == false then
		print("LAG IS HAPPENING")
		
		for i = 1, 25, 0.1 do
			-- math.sin returns a value between -1 and 1 so we first need to add 1 to get a number between 0 and 2, then divide by 2 (multiply by 0.5) to get a number between 0 and 1 so it does not exceed the boundaries we set above
			local position = lowerBound:Lerp(upperBound, (math.sin(i)) * 1)

			-- finally apply our current CFrame to the part's CFrame
			part.CFrame = CFrame.new(position) 

			wait()
		end
		
		isUp = true
	end
	
	
	



	print("ANIMATION FINISHED")
end

Trying to make parts go at differents heights and speed and not start and end at the same time