Lerping positions in x time

How do i lerp a BaseParts position in x time to y position? I want to keep the time taken as accurate as possible to x time.

Example:

local function testlerp(Object:basepart,  EndPosition: Vector3, X:number) 
 --Lerping Object's position to EndPosition in X time (in seconds) 
end

Vector3 already has a lerp function built in which you can see here.

This is how i’d do it:
X is just how far along the path you want to end up, so to do that we want to multiply it by some time value, luckily this is remarkably to do with RunService.

We can take the deltatime (time in between two frames), and multiply the reciprocal desired time by deltatime.

This technically won’t get us to our desired end goal, since we are taking a fraction of the path to the end each step we take but it will get us mostly close enough.

local RunService = game:GetService("RunService")

function LerpObject(Object: BasePart, EndPosition: Vector3, Time: number)
	local RSConnection: RBXScriptConnection
	local TimeElapsed = 0
	
	RSConnection = RunService.RenderStepped:Connect(function (deltaTime: number)
		TimeElapsed += deltaTime
		Object.Position = Object.Position:Lerp(EndPosition, 1/Time * deltaTime)

		if TimeElapsed + .1 >= Time then -- +.1 just to have extra time to get close to the end goal
			RSConnection:Disconnect()
		end
	end)
end

Edit: i just realized this entire time that ive technically been using easing this entire time and this isnt actually what you are asking for; ignore this post

I think you want to use a tween

Tween tweens to one position and doesn’t account for a different position. If you wanted this to work you would have to call a function containing the tween alot of times

This sounds like a tween to the dot, could you explain in greater detail what effect you are trying to achieve? What is the “different position” issue you are running into?

For example, in a lerp its in a repeating type loop like while, for and repeat. Every time it’ll loop it gets the new current position if the position was changed.

In a Tween, you set a locked position then it moves it there. For example, if Object #1 has a position of 10,10,0 then you tween but then Object #1’s position gets changed to 10,11,10 then tween will still play at the position it took not the new one

I forgot but i want it to move in x speed. For example, 5 studs per second

Does adjusting speed actually make them faster?

What you are asking for does not have anything to do with lerp, as lerp does not “animate” to its position. What you want is tweening models.

To summarize tweening models involves welding every descendant of the model to the model’s primary part, unanchoring the model, and then tweening the primary parts position. Here’s a code example:

local tweenservice = game:GetService("TweenService")
local length = 5
local tweeninfo = TweenInfo.new(length, Enum.EasingStyle.Sine, Enum.EasingDirection.In, 0, false, 0)
local targetmodel = workspace.TestModel
local targetCFrame = CFrame.new(0,0,0)

for i, v in pairs (targetmodel:GetDescendants()) do
	if not v:IsA("BasePart") or targetmodel.PrimaryPart == v then return end
	local weld = Instance.new("Weld")
	weld.Part0 = v
	weld.Part1 = targetmodel.PrimaryPart
	v.Anchored = false
end

local tween = tweenservice:Create(targetmodel.PrimaryPart, tweeninfo, {CFrame = targetCFrame})
tween:Play()
tween.Completed:Wait()
--add this if you want the model to stay in place
for i, v in pairs (targetmodel:GetDescendants()) do
	if not v:IsA("BasePart") then return end
	v:FindFirstChildWhichIsA("Weld"):Destroy()
	v.Anchored = true
end

Please let me know if there is anything wrong

Edit:
Change the length, targetmodel, tweeninfo properties, and targetCFrame values according to your game requirements

Issue is i can’t use tweens why? Because of this

Apologies, I don’t entirely comprehend what you are trying to say here. Are you trying to get equivalent of the lerp function’s second argument (the percentage distance towards the target CFrame)?