Smooth Camera Movement Script tweens as if the player were lagging

local TS = game:GetService('TweenService');

local Camera = game:GetService('Workspace').CurrentCamera;
local Character = game:GetService('Players').LocalPlayer.Character;

local CameraDistance = 5.0;
local CameraHeight = 3.0;
local CameraDelay = 0.020;
local CameraTweenTime = 0.200;
local CameraEasingStyle = Enum.EasingStyle.Exponential;

local LastPlayerPosition = Character.HumanoidRootPart.Position;
local LastCameraPosition = Camera.CFrame.Position;
local LastCameraLook = Camera.CFrame.LookVector;

while true do
        local PlayerPosition = Character.HumanoidRootPart.Position;

        if PlayerPosition ~= LastPlayerPosition then
                local CameraPosition = PlayerPosition - Vector3.new(0, -CameraHeight, CameraDistance);

                local CameraTween = TS:Create(Camera, TweenInfo.new(CameraTweenTime, CameraEasingStyle), {
                        CFrame = CFrame.new(CameraPosition, PlayerPosition),
                });
                
                CameraTween:Play();

                LastCameraPosition = CameraPosition;
                LastPlayerPosition = PlayerPosition;
        end

        local CameraLook = Character.HumanoidRootPart.CFrame.LookVector;

        if CameraLook ~= LastCameraLook then
                local CameraLookTween = TS:Create(Camera, TweenInfo.new(CameraTweenTime, CameraEasingStyle), {
                        CFrame = CFrame.new(Camera.CFrame.Position, PlayerPosition),
                });
                
                CameraLookTween:Play();

                LastCameraLook = CameraLook;
        end

        task.wait(CameraDelay)
end

Hi,
I wanted to make the player’s camera move smoothly, and I made this code. Nonetheless, it looks laggy whereas the player moves and I’d like to fix that.
Thanks in advance.

Hi there! I see that you’re trying to make the camera move smoothly in response to the player’s movements. The issue you’re experiencing with the camera looking laggy might be caused by the fact that you’re using the while true loop to continuously update the camera position and orientation. This can cause the program to consume a lot of resources, which can result in lag.

To make the camera movement smoother, I would recommend using a RenderStepped event instead of a while true loop. This way, the camera will only be updated once per frame, which will reduce the amount of resources used.

local TS = game:GetService('TweenService')
local RunService = game:GetService('RunService')
local Camera = game:GetService('Workspace').CurrentCamera
local Character = game:GetService('Players').LocalPlayer.Character

local CameraDistance = 5.0;
local CameraHeight = 3.0;
local CameraTweenTime = 0.200;
local CameraEasingStyle = Enum.EasingStyle.Exponential;
local LastPlayerPosition = Character.HumanoidRootPart.Position

RunService.RenderStepped:Connect(function()
    local PlayerPosition = Character.HumanoidRootPart.Position
    local CameraPosition = PlayerPosition - Vector3.new(0, -CameraHeight, CameraDistance)
    local CameraOrientation = CFrame.new(CameraPosition, PlayerPosition)
    local CameraLook = Character.HumanoidRootPart.CFrame.LookVector

    if PlayerPosition ~= LastPlayerPosition then
        TS:Create(Camera, TweenInfo.new(CameraTweenTime, CameraEasingStyle), {
            CFrame = CameraOrientation
        }):Play()
        LastPlayerPosition = PlayerPosition
    end

    if Camera.CFrame.LookVector ~= CameraLook then
        TS:Create(Camera, TweenInfo.new(CameraTweenTime, CameraEasingStyle), {
            CFrame = CFrame.new(Camera.CFrame.Position, PlayerPosition),
        }):Play()
    end
end)