Issues with camera tracking system

  1. What do you want to achieve?
    Make the camera smooth at tracking the player

  2. What is the issue?
    The camera is extremely laggy when following the players position

  3. What solutions have you tried so far?
    None

I am trying to make a camera system for my game where it follows the player on the X axis but it is extremely laggy and I don’t know how to make it smooth.

image

TrackPlayer Script:

trackingPlayer = nil

wait(3)

for i, player in pairs(game.Players:GetChildren()) do
	trackingPlayer = player
end

while wait() do
	repeat wait() until trackingPlayer ~= nil
	script.Parent.Position = Vector3.new(trackingPlayer.Character.HumanoidRootPart.Position.X, script.Parent.Position.Y, script.Parent.Position.Z)
end

CameraScript LocalScript:

local CameraPart = workspace.CameraPart
local CurrentCamera = workspace.CurrentCamera

repeat wait()
	CurrentCamera.CameraType = Enum.CameraType.Scriptable
until CurrentCamera.CameraType == Enum.CameraType.Scriptable

while wait() do
	CurrentCamera.CFrame = CameraPart.CFrame
end

Change your LocalScript to this:

--//Services
local RunService = game:GetService("RunService")

--//Variables
local CameraPart = workspace.CameraPart
local CurrentCamera = workspace.CurrentCamera

--//Functions
repeat task.wait()
	CurrentCamera.CameraType = Enum.CameraType.Scriptable
until CurrentCamera.CameraType == Enum.CameraType.Scriptable

RunService.RenderStepped:Connect(function()
	CurrentCamera.CFrame = CameraPart.CFrame
end)

And your TrackPlayer Script to this:

--//Services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

--//Variables
local Part = script.Parent
local Vector3New = Vector3.new

--//Controls
local trackingPlayer = nil

--//Functions
task.wait(3)

for i, player in ipairs(Players:GetPlayers()) do
	trackingPlayer = player
end

RunService.Stepped:Connect(function()
	Part.Position = Vector3New(trackingPlayer.Character.HumanoidRootPart.Position.X, Part.Position.Y, Part.Position.Z)
end)

Its still laggy and the players camera is not attached to the player anymore.