Script not working right when transferred to another game

I have a script that makes the players camera shake and fov gets higher the faster the players run. in my game where i originally created this script it works fine, but i wanted to copy it and paste it into my new game and the camera lags behind the player

local player = game.Players.LocalPlayer
local character = player.Character

local lastVelocity = Vector3.new(0, 0, 0)
local intensityMultiplier = 0.015 

local function moveCamera()
	if character then
		local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")

		if humanoidRootPart then
			local velocity = humanoidRootPart.Velocity

			-- Calculate intensity based on velocity magnitude
			local intensity = velocity.Magnitude * intensityMultiplier

			local cam = game.Workspace.CurrentCamera
			local clamp = math.clamp(1 + (intensity - 0.1), 0.1, intensity)


			cam.CFrame = cam.CFrame * CFrame.Angles(
				math.random(1 - clamp, clamp) / 500,
				math.random(1 - clamp, clamp) / 500,
				math.random(1 - clamp, clamp) / 500
			)


			game:GetService('TweenService'):Create(cam, TweenInfo.new(0.2, Enum.EasingStyle.Quint, Enum.EasingDirection.Out, 0, false, 0), {
				CFrame = cam.CFrame * CFrame.Angles(0, 0, math.rad(math.random(-clamp/2, clamp/2)))
			}):Play()

			lastVelocity = velocity
		else
			print('No HumanoidRootPart found')
		end
	else
		print('No character found')
	end
end

-- Continuously move the camera using a RenderStepped loop
game:GetService("RunService").RenderStepped:Connect(function()
	local humanoidRootPart = character and character:FindFirstChild("HumanoidRootPart")
	if humanoidRootPart then
		if humanoidRootPart.Velocity == Vector3.new(0, 0, 0) then
			-- Reset lastVelocity to zero when the player's velocity is zero
			lastVelocity = Vector3.new(0, 0, 0)
		end
		if humanoidRootPart.Velocity ~= lastVelocity then
			-- Move the camera when velocity changes
			moveCamera()

		end
	end
end)

local PS = game:GetService("Players")
local RNS = game:GetService("RunService")

local player = PS.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local HRP: BasePart = char:WaitForChild("HumanoidRootPart")
local camera = workspace.CurrentCamera
local divisor = 5 -- the dampening of the dynamic fov
local lastVelocity = HRP.AssemblyLinearVelocity.Magnitude / divisor
local defFov = 70

local function Lerp(a, b, t)
	return a + (b - a) * t
end

RNS.RenderStepped:Connect(function()
	camera.FieldOfView = defFov + Lerp(lastVelocity, HRP.AssemblyLinearVelocity.Magnitude / divisor, 1)
	lastVelocity = camera.FieldOfView - defFov
end)

I dont understand why in my 2nd game the camera lags behind the player but in my 1st game its smooth and working fine.

1 Like

Publishing my game fixed the issue

1 Like

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