For loop when wait() added will break CFrame

Hi! I am currently experiencing an odd issue for me regarding for loops, lerps, and Cframes.

Here is the code including the problem:

local camera = workspace.CurrentCamera

local player = game:GetService(“Players”).LocalPlayer

local char = player.Character or player.CharacterAdded:wait()

local runService = game:GetService(“RunService”)

local uis = game:GetService(“UserInputService”)

local humanoid = char:FindFirstChild(“Humanoid”)

runService.RenderStepped:Connect(function()
if humanoid then

	if uis:IsKeyDown(Enum.KeyCode.Q) then
		local pos = camera.CFrame* CFrame.Angles(0,0,math.rad(-2))
		for i = 0,1,.01 do
			local lerp = camera.CFrame:Lerp(pos,i)
			camera.CFrame = lerp

			wait() -- the problem
			
		end
	end
end

end)

__

My main goal was to tilt the camera influenced by the players walk direction.
Without “wait()”, it will tilt the camera but it would not be smooth. But when wait() is added inside the for loop, it would glitch instead and not tilt (The camera would lock its position for a few seconds until for i is done).

Had this problem before it’s because you’re running this every frame

runService.RenderStepped:Connect(function(deltaTime)
	if humanoid then
		if uis:IsKeyDown(Enum.KeyCode.Q) then
			local pos = camera.CFrame* CFrame.Angles(0,0,math.rad(-2))
			local lerpIntensity = 0.1 -- lerp intensity i guess?
			
			-- Big note here:
			-- Since this is running every frame, the higher framerate you have, the faster the camera will tilt
			-- This includes players with FPSUnlocker
			camera.CFrame = camera.CFrame:Lerp(pos, 0.1) 
		end
	end
end)

I greatly appreciate your answer but i wanted the camera to tilt smoothly so in my script (the one that errors), I added a for loop to the lerping of the CFrame. I apologize if i misunderstood something though :slight_smile:

1 Like