Trying to understand this function completely

Could someone help me understand this completely, I do understand a lot but not really how the mathematical calculations work and the base:Lerp, also the dt and alpha

Thank you.

function resizeModel(model, a)
	local base = model.PrimaryPart.Position
	for _, part in pairs(model:GetDescendants()) do
		if part:IsA("Part") or part:IsA("MeshPart") then
			part.Position = base:Lerp(part.Position, a)
			part.Size *= a
		end
	end
end

function tweenModelSize(model, duration, increment, easingStyle, easingDirection)
	local s = increment - 1
	local i = 0
	local oldAlpha = 0
	while i < 1 do
		local dt = RunService.Heartbeat:Wait()
		i = math.min(i + dt/duration, 1)
		local alpha = TweenService:GetValue(i, easingStyle, easingDirection)
		resizeModel(model, (alpha*s + 1)/(oldAlpha*s + 1))
		oldAlpha = alpha
	end
end
2 Likes

Did you create the script yourself or is it a free model?

1 Like

Copied it from another devforum post cause I really needed it, but I want to understand it better.

Would you mind linking the original post where you found it?
Some context might help with figuring out certain aspects

Let’s first look at the resizeModel function:

function resizeModel(model, a)
	local base = model.PrimaryPart.Position 
	for _, part in pairs(model:GetDescendants()) do
		if part:IsA("Part") or part:IsA("MeshPart") then
			part.Position = base:Lerp(part.Position, a)
			part.Size *= a
		end
	end
end

In this function is model the model to be scaled and a is the product to scale the model by, so if a is 2 the model will be two times bigger

first the function stores the position of the PrimaryPart of the model in the base variable. This is where the model will be scaled from

local base = model.PrimaryPart.Position

Than it loops through all the parts in the model:

for _, part in pairs(model:GetDescendants()) do
	if part:IsA("Part") or part:IsA("MeshPart") then

Than the function takes the position of the part and lerps it. Lerping is a function that can easily scale a position by alpha (a). What does that mean? Lerp is a function that looks something like this:

local function Lerp(Start, Finish, Alpha)
	return Start + Alpha * (Finish - Start)
end

I will explain this function with my beautiful drawing:

Lerp calculates the direction from a to b. (Finish - Start) which is the arrow or c in the drawing. Than it multiplies that by a number alpha or a in the function. If alpha is 0.5, the direction will be cut in half and if alpha is two, the direction will be twice as big. That direction will be added to the Start, which is a in the drawing and the PrimaryPart of the model in the function. Than the part will be multiplied by a which makes the part a times bigger

part.Size *= a

This can also be written as:

part.Size = part.Size * a

In the next function (tweenModelSize), the model is tweened.
Let’s first look at the while loop.

local dt = RunService.Heartbeat:Wait()

This line will make the script wait for one frame. If you wanna learn more about RunService and Heartbeat, check out this article. dt is the time since the last frame.

i = math.min(i + dt/duration, 1)

i is how much the tween has progressed, so if i is 0.5, the tween is at half the duration. math.min will make sure that i does not exceed 1. In this line, i is incremented by the the amount that has passed since the last frame divided by the duration of the tween. If the tween lasts ten seconds (duration = 10) and dt is 0.1 than i will increase by 0.1 / 10 which is 0.01.

local alpha = TweenService:GetValue(i, easingStyle, easingDirection)

alpha is the number that will be used to resize the model. The TweenService:GetValue function returns a new alpha value with the given easingstyle and direction.

resizeModel(model, (alpha*s + 1)/(oldAlpha*s + 1))

This is a little bit complicated. Because the function updates the size of the model every frame, you can’t just do resizeModel(model, alpha), because than the model will explode in size. so the size of the model is increased by the current a divided by the second a. I’m not sure myself why s = increment - 1 and why you add 1 to alpha * s.
There also is a slight mistake in the resizeModel function. This line:

part.Position = base:Lerp(part.Position, a)

should be

part.Position = base:Lerp(part.Position, a - ((a - 1) / 2))

I don’t have time to explain this right now, but if you want explanation, I can give that a little later. if you have any other questions, or if you have questions about my explanation, feel free to ask anything you want.

3 Likes

Thanks really good explanation, got to understand a lot more!