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