Is Awaiting Heartbeat too Intensive?

Say for instance you wanted to begin a CFrame lerp for multiple parts in one go at random times. What would the most resourceful/efficient method be?

I want to use:

local parts = {...};

function lerp()
        local a = 0;
        
        repeat
                local dt = game:GetService'RunService'.Heartbeat:Wait();
                for i = 1, #parts do
                        a += dt;
                        parts[i].CFrame += CFrame.new(0, dt * 30, 0);
                end
        until a >= 1; --each part escalates by 30 studs in approx. 1 second
end

Or would using the event be better?

local a, parts = 0, {...};

game:GetService'RunService'.Heartbeat:Connect(function(dt)
        if a <= 0 then return; end;
        a -= dt;
        for i = 1, #parts do
                parts[i].CFrame += CFrame.new(0, dt * 30, 0);
        end
end)

function lerp(s)
       a += s;
end

If you have a better method feel free to share!

I’d use the event, just because its a little bit easier to use in my opinion. Heartbeat performance is purely based off of what code is within it. Moving a couple hundred parts via CFrame should be nothing to worry about.

1 Like

They both seem pretty similar - I doubt you’d see much difference, though I’d disconnect the event when you’re done with it rather than returning nil forever after its use is complete.

Connect it each time you need it as part of the lerp function.

Assuming lerp() is being called indefinitely, you wouldn’t disconnect the heartbeat, right?

Right, you wouldn’t disconnect it. You would just disconnect it if you ever didn’t need it. It’s easier than trying to break a loop with a variable, which is why I’d recommend it.