RenderStepped (setting part's CFrame) causes very high lag

Hello everyone,
Earlier this week I encountered this issue with RenderStepped. When it’s ran, it highly decreses FPS in-game. Now this may be because of too many RenderStepped usages in the game overall, and if it is, what are alternative ways to optimize it?

Example of RenderStepped lag (placeholder trap’s CFrame is getting set in front of the player using RenderStepped) (look at FPS in the bottom right corner):

Edit: It only lags when setting CFrame/Position of something.


This is happening with the flashlight also.

3 Likes

I would personally recommend using the “:GetPropertyChangedSignal()” Event for CFrame changes, this allows the CFrame to update only when it needs to.

1 Like

Code is needed to figure out what’s actually going on. If you want to test it further, try reproducing the issue in a minimal test place with everything else stripped away for simplicity.

1 Like

I’ll give it a try, I suppose.

1 Like

Tried, same outcome nothing happened.

1 Like

It’s not going to lag in a smaller game, it’s literally just setting a part’s CFrame. Here’s a snip of the code:
Placeholder trap script:


Here’s an even simpler one:

1 Like

Btw, RenderStepped is getting deprecated eventually, PreRender is the alternative, dont know if this will help though.

It has nothing to do with setting positions. It’s because you have an intensely detailed game and you’re using RayCasts.

RayCasts have to check every item that’s in the whitelist or every item that isn’t excluded/blacklisted which leads to your lag. That’s your issue.

As for the flashlight, why are you spam creating TweenInfos? You can save them to a variable and reuse them.

Well that’s new to hear, I’ll have a look into that

It’s sort of how I’ve always done it, never really gotten any issues.
But I’ll give it a try, might not lag then,

Suppose I’ll just remove the raycast from that.

Edit1: still lags for the flashlight.

Yeah, i had found out randomly while searching for smth about RunService.

I take it you are still tweening in the flashlight code? I agree that tweening / ray casting is probably the issue here and you should try to remove both from happening every frame.

Render stepped isn’t supposed to be for heavy calculations and will definitely cause lag. Instead do your calculations in Stepped which has a bit more leeway.

I’ve already tried replacing tweening with just PivotTo(), it’s the same outcome.

Tried .Stepped, still the same.

The problem is not with the CFrame, but with Raycast. Doing Raycast 60 times per second is very resource consuming. You should probably add a delay so that is checks this condition every 0.05 secs or so.
For example,

local lastRaycast = 0
prevConnection = RunService.Heartbeat:Connect(function() --Heartbeat is basically the same as RenderStepped
  if (tick()-lastRaycast >= 0.05) then
    lastRaycast = tick()
    -- blah blah blah
  end
end)
1 Like

The flashlight does not use raycast, however the raycast one I can just replace. So it’s fine.

Maybe the issue lies somewhere else? CFraming a part every frame should be perfectly fine.

You could try replacing RenderStepped with Heartbeat. RenderStepped should only be used for updating the camera or character model, and running computationally expensive operations in RenderStepped can significantly slow the game down (even though I doubt you’re doing anything extremely expensive).

I doubt raycasting is the cause of the performance issues. Raycast performance is dependent on the length of the ray used, and OP is only using 3 stud long rays.

The best way to debug this would be to place a bunch of debug.profilebegin() calls around parts of your code you think cause the issue and open the MicroProfiler to check if they really are an issue.

1 Like

I’ve never used that, how would I use it in a script?

See: Using MicroProfiler | Documentation - Roblox Creator Hub

The MicroProfiler may be a little hard to read at first, so this is a good video demonstrating how it works: https://www.youtube.com/watch?v=BbIPalpAfaI