Hello all!
Today I have made a module that has timed Linear Interpolation for all properties of the part.
Link : Lerping Module - Roblox
Code
local RunService = game:GetService("RunService")
local Lerp
Lerp = function(Part : PVInstance, Property : string, Goal, Time : number)
local TimePassed = 0
local Start = Part[Property]
while TimePassed <= Time do
TimePassed += RunService.Heartbeat:Wait()
Part[Property] = Start:Lerp(Goal, TimePassed/Time)
end
end
return Lerp
Example code:
local Lerp = require(script.ModuleScript)
local PartToLerp = workspace[YOURPARTHERE]
local Property = "YOURPROPERTYTOLERP"
local Time = YOURLENGTHOFLERP
local Goal = YOURLERPGOAL
Lerp(PartToLerp, Property , Goal, YOURLENGTHOFLERP)
Position Example Code:
local Lerp = require(script.Lerp)
Lerp(workspace.LerpingPart, "Position" , Vector3.new(48, 34, 34), 3)
Hope you enjoy!
3 Likes
Well, as far as I’m aware, lerping is more efficient than tweening for linear interpolation.
You should add tweenInfos for these lerps.
I don’t think lerping is necessarily better than tweening, why do you say it is more efficient? From experience I believe they aren’t as smooth, plus they don’t support easing styles. I could be wrong so just trying to understand, maybe you can provide a comparison?
That’s why I pointed out linear interpolation. Linear interpolation is for when you simply wanna change a value smoothly.
And yes, I’m pretty sure it’s more efficient. I can’t state it as a fact, though.
Btw @Tomroblox54321, can you please put the model out for sale, think it’s still private.
Not all properties have access to a built in Lerp function, so this only works with properties that support it (Color3, Vectors, CFrames, etc)
A lerp function is also, as the name entails - Linear. Which means you have no control over the transition rate
TweenService offers more control and is generally comparable in performance. Reinventing the wheel when it’s supported in-engine is usually bad practice
Use TweenService.GetValue if you want to add customizable easing styles to the module. Instead of a while true loop with a Heartbeat:Wait() just use a heartbeat connection and disconnect it when the time goes over what it should be.
Made it on-sale. Not sure what happened there.
I’ll try to add easingstyles aswell.
Lerps cause less lag and are better since they are based off of math.
I’d use Stepped instead of Heartbeat, but that is a personal choice.
I tried using Stepped for this module instead of heartbeat but it entirely broke it.