So I have this very long RenderStepped function that is called with :Connect(). My game is having some frame drop issues, and I have concluded that it comes from this loop. In this loop I do a lot of lerping to make my GUI look smooth, but the most important is for handling the view-models of my game. There’s a lot of other stuff there that I just added because I was scared of making a separate while loop that runs slower to handle these things.
So my question is, would performance improve if I chunked this very long loop into a RenderStepped and a while loop that runs every, let’s say, 0.1 seconds? I also considered switching this to Heartbeat instead. What difference would that make?
Yeah, running things less improves performance most of the time (but not all). Have you tried using debug.profilebegin(), debug.profileend(), and the microprofiler? You can access it in studio by pressing Ctrl-F6 and then pause it to view the result with Ctrl-P. Just throw in a bunch of calls to debug.profilebegin with corresponding debug.profileend calls at key point as seen in this tutorial: Using MicroProfiler | Documentation - Roblox Creator Hub
(It looks really pretty too. The guy who made it is a genius.)
I’m suspicious that there’s something deeper going on here. Updating GUI positions at 10 hz would be visually unacceptable. Have you tried using the microprofiler to figure out exactly what the bottleneck is?
Post it. No one is going to be able to help you until you post a relevant sample of code.
Does TweenService not work for your use case? I think you might be reinventing the wheel unnecessarily here. Posting the code would help determine that.
No it wouldn’t, because you’d be running the same code except in overlapping succession.
You want to be extremely selective about what code you run on RenderStepped, because your frame will not render until execution of this code completes. Executing too much code in RenderStepped will directly impact your game’s framerate. You should look at all the code you’re running on RenderStepped, and any of the code that does not need to run lockstep with the frame rendering should be moved to execute on Heartbeat instead. The only code that should run on RenderStepped is code that directly manipulates the rendering camera (such as setting CurrentCamera.CFrame), or code that positions things relative to the camera (e.g. a weapon a first-person shooter) and must not lag a frame behind. If you’re using RenderStepped for your main game logic loop, or for UI updates, that is going to be terrible for performance.
Don’t use while loops for game loops or animation. Use while loops for iterating, logic, algorithms, etc. as you would for loops. Use the RunService events for all long-running game loops.
BindToRenderStep still runs all the bound code on RenderStep, so it will not fix the original problem. What it does get you over RunService.RenderStepped:Connect(function) is fine control over when, during the RenderStep, the bound code runs relative to other code also running on RenderStep. The most important usage of this is when you want code to run explicitly before or after the camera CFrame update for the next frame that will be rendered.