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.
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.
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.
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)
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.