So I’ve been trying to fix this issue for about a month, but it doesn’t seem to work, decided to use chatgbt to fix the issue but still didn’t work. The issue is that when you join the game, the camera starts out fine, but when you continue playing, the camera or framerate starts to lag, this only happens when I seem to try to add any form of deltatime into the camera script. This issue also amplifies on higher framerates.
The code:
local RunService = game:GetService("RunService")
local RS = game:GetService("ReplicatedStorage")
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local root = character:WaitForChild("HumanoidRootPart")
local __init__ = script.Parent
local event = __init__.events.changeCamera
local cameraPart = RS:FindFirstChild("C"):Clone(workspace)
local camera = workspace.CurrentCamera
local camera_subject = root
local camera_x = 0
local camera_y = 0
local cameraSpeed = 0.5
local lastFrame = tick()
local min_Threshold = 0.1
--Function-Called-------------------------------------------------------------------------------
event.OnClientEvent:Connect(function(player, part)
camera_subject = part
camera.CFrame = camera_subject
end)
--OnFrame-------------------------------------------------------------------------------
local function onFrame()
local deltaTime = tick() - lastFrame
lastFrame = tick()
local targetX = camera_subject.Position.X
local targetY = camera_subject.Position.Y + 2
local dx = targetX - camera_x
local dy = targetY - camera_y
if math.abs(dx) > min_Threshold or math.abs(dy) > min_Threshold then
camera_x = camera_x + dx / cameraSpeed * deltaTime
camera_y = camera_y + dy / cameraSpeed * deltaTime
end
cameraPart.Position = Vector3.new(camera_x, camera_y, 40)
cameraPart.Orientation = Vector3.new(0,0,0)
camera.CFrame = cameraPart.CFrame
camera.CameraType = Enum.CameraType.Scriptable
root.CFrame = CFrame.new(root.CFrame.X, root.CFrame.Y, 0)
end
---------------------------------------------------------------------------------
RunService.Heartbeat:Connect(onFrame)
Video of the issue:
thank you