Speed of camera and part changes by the FPS

As soon as I changed my FPS cap from 60 to 120 (with my mouse being outside of the screen). The camera changes its speed and the moving rainbow city lights also change their speed. How do I prevent this?

Camera script (local script):

local player = game.Players.LocalPlayer
local part = game.Workspace.Part
local spawnlocation =  game.Workspace.fd
local camera = workspace.CurrentCamera
camera.CameraType = "Scriptable"

while true do

	for i = 1, 360, 0.1 do
		task.wait()
		camera.CFrame = part.CFrame
		part.CFrame = CFrame.new(spawnlocation.Position.X,spawnlocation.Position.Y + 400,spawnlocation.Position.Z) * CFrame.fromOrientation(0,math.rad(i),0) * CFrame.new(690,60,0) 
		part.CFrame = CFrame.lookAt(part.Position, spawnlocation.Position)
	end
end

Rainbow Light Script (only one) (also local):

local hinge = game.Workspace.LightLauncher1.Hinge1
local light = game.Workspace.LightLauncher1.mover
local RunService = game:GetService("RunService")
local hingepos = hinge.Position

local test
local firsttime
while true do
	
	if test == nil then
		test = 0
		firsttime = 70
	end

	if test == -70 then
		firsttime = 140
	end
	for i = 1, firsttime do

		light.CFrame = CFrame.new(hingepos) * CFrame.Angles(math.rad(test) + math.rad(i),0,0) * CFrame.new(0,5,0)
		task.wait()

	end


	for i = 1, 140 do

		light.CFrame = CFrame.new(hingepos) * CFrame.Angles(math.rad(70) + math.rad(-i),0,0) * CFrame.new(0,5,0)
		task.wait()

	end

	test = -70

end

Heartbeat is framerate dependent so if the fps changes then the time waited will change as well which is the issue here.

To solve this for the first camera script it’s best to use deltatime like so instead of relying on task.wait() to wait a certain amount of time.

Where elapsed time is the deltatime where the script will need to adjust.

local rotationAmount = 0
while true do
local deltaTime = 		task.wait()
rotationAmount += deltaTime
local i = rotationAmount --replace the forloop i with total time accumulated
		camera.CFrame = part.CFrame
		part.CFrame = CFrame.new(spawnlocation.Position.X,spawnlocation.Position.Y + 400,spawnlocation.Position.Z) * CFrame.fromOrientation(0,math.rad(i),0) * CFrame.new(690,60,0) 
		part.CFrame = CFrame.lookAt(part.Position, spawnlocation.Position)
	end
end

2 Likes