RunService optimization

local ShiftLockController = {}

-- Services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local GameSettings = UserSettings().GameSettings

-- Constants
local CAMERA_OFFSET_ENABLED = Vector3.new(0, 1.7, 0)
local CAMERA_OFFSET_DISABLED = Vector3.new(0, 0, 0)
local TRANSITION_SPEED = 5

local Spring = require(script:WaitForChild("Spring"))

-- Variables
local Player = Players.LocalPlayer
ShiftLockController.IsEnabled = false
local currentCameraOffset = CAMERA_OFFSET_DISABLED

local function updateCameraOffset(dt) --Delta time
    local character = Player.Character or Player.CharacterAdded:Wait()
    local humanoid = character and character:FindFirstChildOfClass("Humanoid")
    if humanoid then
        -- Linearly interpolate the camera offset towards target
        currentCameraOffset = currentCameraOffset:Lerp(
            ShiftLockController.IsEnabled and CAMERA_OFFSET_ENABLED or CAMERA_OFFSET_DISABLED,
            dt * TRANSITION_SPEED
        )
        humanoid.CameraOffset = currentCameraOffset
    end
end

function ShiftLockController.Enable()
    if not ShiftLockController.IsEnabled then
        ShiftLockController.IsEnabled = true
        UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
        GameSettings.RotationType = Enum.RotationType.CameraRelative
    end
end

function ShiftLockController.Disable()
    if ShiftLockController.IsEnabled then
        ShiftLockController.IsEnabled = false
        UserInputService.MouseBehavior = Enum.MouseBehavior.Default
        GameSettings.RotationType = Enum.RotationType.MovementRelative
    end
end

--Loop to smoothly update camoffset
RunService.Heartbeat:Connect(function(dt)
    updateCameraOffset(dt)
end)

return ShiftLockController

Would i have to be concerned about performance issues whilst using this heartbeat connection?And if so, which would be the best solution:

  1. Managing the connection with maid
  2. Using a coroutine for a smooth transition
  3. Using spring (I don’t really know how this module works, but I’ve heard about it)
  4. Other

Thx

I would recommend reading up on this article (Task Scheduler | Documentation - Roblox Creator Hub).

It is recommended to do something like a custom camera system in the Rendering category, more specifically BindToRenderStep. The default Roblox camera uses BindToRenderStep with the priority as Enum.RenderPriority.Camera.Value, meaning you can override or modify the default camera behavior just directly after with Enum.RenderPriority.Camera.Value + 1. There should be no issues with performance unless you are doing some real heavy work every event (which you are not).

As for your other concerns,

  1. Managing the connection with maid: You can technically use Maid, but in your case it is as simple as BindToRenderStep/UnbindFromRenderStep that I would say it’s just overkill to use it.
  2. Using a coroutine for a smooth transition: I don’t really understand what you mean by this. Heartbeat/RenderStep already fires every 1/FPS seconds, so it’s already pretty smooth if your FPS is high.
  3. Using spring: Totally up to you if you do want to use spring for a more realistic effect. If you are using something like Quenty’s Spring module, you would still need to step to the next calculations by binding it to an event like RenderStepped.
1 Like

Ok thank you ill look into it for sure

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