RunService Frame Rate Issue

Hello Everyone, so, I’m making right now game, that have Viewmodels.
And my friend saw that, if you will change the Maximum Frame Rate in the Settings, viewmodel swaying would be a lot faster with the, for example 240 Maximum Frame Rate. When, using the 60 Maximum Frame Rate it’s going slightly faster.

And I know that problem, but I didn’t know how to fix it, so I started searching and…
Found post that talking about RunService.PostSimulation, PreSimulation, etc.

I tried it, and, it works, maybe, but it’s LAGGING. Like when I’m walking so the viewmodel can sway.
The viewmodel is literally lagging, and I don’t know how to fix it.

Here is the script (not full):

function Framework:UpdateModelPosition(deltaTime: number?)
	local character: Model? = self.Player.Character or self.Player.CharacterAdded:Wait()
	local primaryPart: BasePart? = self.HandModel.PrimaryPart
	if not self.HandModel or not primaryPart or not character then return end

	local hum: Humanoid? = character:WaitForChild("Humanoid")
	if not hum then return end
	self.Walking = hum.MoveDirection.Magnitude > 0

	self.GlobalDivider = self.Lerp(1, 3.25, 1.5)
	local divider = self.GlobalDivider

	local CameraCFrame: CFrame = self.Camera.CFrame
	local newCamera: Attachment? = primaryPart:WaitForChild("Attachment")
	local rootPart: BasePart? = character:WaitForChild("HumanoidRootPart")

	local currentTime = tick()
	local delta = self.GetClippedMouseDelta(CameraCFrame)
	self.Springs.Sway:shove(Vector3.new(delta.X / 150, delta.Y / 200, 0))
	self.Springs.Sway.Position = Vector3.new(
		math.clamp(self.Springs.Sway.Position.X, -1.5, 1.5), 
		self.Springs.Sway.Position.Y, 0
	)
	local Sway = self.Springs.Sway:update(deltaTime)
	local SwayVector = Vector3.new(0, Sway.Y / 2, 0) / divider
	local SwayRotation = Vector3.new(-Sway.Y * 2, 0, Sway.X / 2) / divider

	local speed: number = hum.WalkSpeed
	if self.Walking then
		self.Walk = self.Walk:Lerp(CFrame.new(
			0.025 * (speed / 10) * math.sin(currentTime * 8),
			0.025 * (speed / 10) * math.cos(currentTime * 16), 0
			) * CFrame.Angles(
				math.rad(1 * (speed / 10) * math.sin(currentTime * 16)),
				math.rad(1 * (speed / 10) * math.cos(currentTime * 8)), 0
			), 0.1 * deltaTime)
	else
		self.Walk = self.Walk:Lerp(CFrame.new(0, -hum.CameraOffset.Y / 2, 0), 0.1 * deltaTime)
		self.Walk = self.Walk:Lerp(CFrame.Angles(
			math.cos(currentTime * 2) * 0.01, 
			math.cos(currentTime * 3) * 0.01, 0
			), 0.1 * deltaTime)
	end

	local function GetRollAngle()
		if not character or not hum then return end
		return -CameraCFrame.RightVector:Dot(hum.MoveDirection)
	end
	local RollViewmodel = GetRollAngle() * 4.5
	self.Rotate = self.Rotate:Lerp(CFrame.Angles(0, 0, math.rad(RollViewmodel)), 0.075 * deltaTime)

	local rayOrigin, rayDirection = CameraCFrame.Position, CameraCFrame.LookVector * 5
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {self.HandModel, character}
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	raycastParams.IgnoreWater = true

	if primaryPart and newCamera then
		local rightArm: BasePart? = primaryPart.Parent["Right Arm"]
		if not rightArm then return end

		local viewmodelCFrame: CFrame = primaryPart.CFrame
		local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
		if raycastResult then
			local intersectionPoint = raycastResult.Position
			local offsetDirection = (rayOrigin - intersectionPoint).Unit
			local newPosition = rightArm.CFrame.Position + offsetDirection * 0.5
			viewmodelCFrame = CFrame.new(newPosition)
		end
		local SwayCFrame = CFrame.new(SwayVector) * CFrame.Angles(
			SwayRotation.X, SwayRotation.Y, SwayRotation.Z
		)
		if not SwayCFrame then return end
		CameraCFrame = CameraCFrame * self.Walk
		primaryPart:PivotTo(
			CameraCFrame * SwayCFrame * self.Walk * self.Rotate * newCamera.CFrame:Inverse()
		)
	end
end
RunService.PostSimulation:Connect(function(deltaTime)
	self:UpdateModelPosition(deltaTime)
end)

If someone can help, that would be REALLY appreciated!
Thanks.

3 Likes

Fixed, for the curious one that have the similar problem, I just used PreRender instead of RenderStepped, and multiplied deltaTime to 4-6, so I can use it as a alpha for a Lerps, to get result that I want.

1 Like

You can use this lerp formula as well

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.