Is my ViewModel CFraming efficient?

Of course, viewmodel = fps arms.

Anyways, I wrote this viewmodel CFraming code, and I noticed that some stuff would go kinda slow when running. What I mean by slow is that the viewmodel lags a little. Keep in mind that the only stuff in the background are small scripts made by me, so that isn’t causing things to go slower.

Here’s my code:

runServ.RenderStepped:Connect(function(deltaTime)	
	
	--//Primary CFraming\\--
	
	Springs.sway = Springs.create()
	Springs.walkCycle = Springs.create() --//Currently not in use
	
	if aiming == false then
		--Springs.sway.k = 0.05
		--Springs.sway.friction = 0.1
		
		--Springs.sway.Speed = 4
		
		local mouseDelta = UIS:GetMouseDelta()
		Springs.sway:shove(Vector3.new(mouseDelta.x / 250, mouseDelta.y / 250)) --not sure if this needs deltaTime filtering
	elseif aiming == true then
		--Springs.sway.Speed = 50
		
		local mouseDelta = UIS:GetMouseDelta()
		Springs.sway:shove(Vector3.new(mouseDelta.x / 150, mouseDelta.y / 150))
	end

	local sway = Springs.sway:update(deltaTime)	

	Primary.PrimaryPart.CFrame = Camera.CFrame * Main
	Primary.PrimaryPart.CFrame = Primary.PrimaryPart.CFrame * CFrame.Angles(0,-sway.x,sway.y)

	if PrimaryEquipped == true and aiming == true then
		Main = Main:Lerp(CFrameDataPrimary.ads, 0.1)
		Primary:SetPrimaryPartCFrame(Primary:GetPrimaryPartCFrame():Lerp(camera.CFrame * Main, 0.6))
	elseif PrimaryEquipped == true then
		Main = Main:Lerp(CFrameDataPrimary.main, .2)
	end
end)

Yes, it uses a separate spring module and settings module, but that shouldn’t be causing the problem.

Yes, I am 100% sure it is not my computer, as for I have a very nice GPU (Radeon RX 570 Series).

Yes, it not my network.

Yes, it isn’t stuff running in the background.

(This code does work and runs almost perfectly most of the time.)

This code is part a 600+ line script, and I have tried disabling that part. Nothing helped.

Any advice would be appreciated! :smiley:

2 Likes

Be careful when subscribing to RenderStepped:

code connected to RenderStepped must be executed prior to the frame being rendered. This can lead to significant performance issues if RenderStepped is used inappropriately. To avoid this, only use RenderStepped for code that works with the camera or character. Otherwise, RunService.Heartbeat should be used.

As a first attempt, try using RunService.Stepped instead of RenderStepped.

Also,

Springs.sway = Springs.create()

Are you certain you mean to create a new spring every frame? That’s a lot of memory allocation that you probably don’t need. Wouldn’t it make sense to make the spring once, and then just call methods on it each frame?

4 Likes

Also, be warned that setting primary part CFrame like this will cause parts in the model to slowly drift apart, due to rounding errors. See here for more discussion and solutions.

3 Likes

Yes, sadly, I do have to create a new spring every renderstep because it breaks when I don’t create one every single time. I’ll look farther into it!