Camera goes into the sky

So, I’m trying to make a 2D camera, but I’m having trouble making the camera’s CFrame the same as the part. The Camera goes to the sky instead of the part.
Scripts
(Local script in StarterPlayerScripts)

wait(2)
game.Workspace.Camera.CameraType = Enum.CameraType.Scriptable
game.Workspace.Camera.CFrame = game.Workspace.Cam.CFrame

and
(Local script in StarterPlayerScripts)

while true do
    wait(.01)
    game.Workspace.Cam.Position = game.Players.LocalPlayer.Character.HumanoidRootPart.Position + Vector3.new(0,0,10)
end

Although the second script works, I wonder if its messing up the other one as when I deleted it, the first one works fine.

Assuming I’m understanding the problem correctly, you’re wanting a part to constantly be 10 studs above the player, and the player’s camera to have the same CFrame as the part (looking down)

This code below should work if the “Cam” part is Anchored and it’s in a LocalScript

local RunServ = game:GetService("RunService")
local Player = game.Players.LocalPlayer
local Part = workspace.Cam
local Camera = workspace.CurrentCamera
local AddVector = Vector3.new(0,10,0) --This is how far above the block will go, read somewhere that setting this as a variable is more optimized than doing Vector3.new in the loop

RunServ.RenderStepped:Connect(function() --Renders every frame, more efficient than "while true do" 
	if Player.Character and Player.Character:FindFirstChild("HumanoidRootPart") then
		Part.CFrame = CFrame.new(Player.Character.HumanoidRootPart.Position+AddVector,Player.Character.HumanoidRootPart.Position) --Sets the part's position (first parameter) and makes it look at the player's RootPart (second parameter)
		Camera.CFrame = Part.CFrame --Sets the camera's CFrame to the part's CFrame
	end
end)

Also, additional tip, you can do “while wait(0.01) do” instead of

1 Like

Thanks, this works! I also made my own version of this which is only 7 lines! Would you like to see it?