Tween Service vs. CFrame:lerp()

Let’s brief y’all on the scenario before I give you my question. I have a gun system that uses the following to move the bullet (it is designed to reflect based on Crazyman32’s Tutorial):

-- Cast ray:
local ray = Ray.new(currentPos, currentNormal * (overrideDistance or stepDistance));
local hit, pos, norm = game.Workspace:FindPartOnRayWithIgnoreList(ray, globalIgnoreList);
					
-- Update laser position:
bullet.Size = Vector3.new(0.15, 0.15, ((pos - currentPos).magnitude));
bullet.CFrame = CFrame.new(currentPos:lerp(pos, 0.5), pos);
					
local oldPos = currentPos;
currentPos = pos;
					
-- If hit do...
if (hit) and (stepCM <= stepMax) then
	stepCM = stepCM + 1;
	hitpart = hit;
						
	-- Can we reflect it?
	local canRef = bulletHit(currentPos,hitpart,currentNormal,settings);
	if canRef == false then bullet:Destroy(); return end
												
	-- r = d - 2(d DOT n)n
        -- Reflect:
	local reflect = (currentNormal - (2 * currentNormal:Dot(norm) * norm));
	currentNormal = reflect;
						
	Step(stepDistance - (pos - oldPos).magnitude);
	return;
end

Essentially, I want to know if lerping with a CFrame is going to put more stress on the server than if I was using a Tween. (Which would also personally be easier for me to calculate mathematically)

I will take anything, no matter how difficult, to reduce stress on the server by the highest amount.

Note: For anyone questioning as to why the calculations and effects are done on the server, it’s for a multitude of reasons. Thank you.

5 Likes

I think that CFraming May stress the server more. I may be wrong but this is my honest opinion. I would use tween service.

I think that TweenService has a ton of overhead created along with tweening the cframe. Lerping is just using a function to interpolate between two oriented points and doing it repetitively.

e.g.

For TweenService:

  1. Create CFrameValue
  2. Set its starting value
  3. TweenService:Create is called, returning a tween
  4. Tween is immediately played

For Lerping:

  1. Create CFrame variable
  2. CFrame:Lerp is called 60 times/sec, returning a lerped CFrame, in which:
    a. CF1*(1 - a) + CF2*(a) as reference function for position
    b. probably some trigonometric function/identity to solve for angle vectors.

Less steps usually mean less computation, and less instantiating of objects also mean less for the memory to account for.

3 Likes

The computational expense involved behind the TweenService API is negligible in comparison to lerp, if there is any in the first place. I generally don’t agree with the sentiment that “less steps equals less computation”, it’s not necessarily substantiated.

Just let the client handle any kind of tween work, as it should. The server won’t waste memory trying to create an effect and the server doesn’t need to stress with something that doesn’t affect it. If everyone needs to see it, use a remote and let the other clients handle the parts.

27 Likes

I’m going to mark this as the solution because I came up with a solution using what you’ve given me.

3 Likes