Locking camera to part CFrame

Hello guys! I’m trying to set the camera to a part, but I’m having trouble!

local camera = game.Workspace.Camera

local cameraTesting = workspace.CameraTest1
local cameraTesting2 = workspace.CameraTest2


function setCameraBeforeAnimation(part)
	camera.CameraType = Enum.CameraType.Scriptable
	camera.CFrame = part.CFrame
	print("success")
end


setCameraBeforeAnimation(cameraTesting)

I’m unsure as to why this isn’t working.

You need to add a small wait so that the camera can actually load before setting the camera’s properties.

This should work:

local camera = workspace.CurrentCamera

local cameraTesting = workspace.CameraTest1
local cameraTesting2 = workspace.CameraTest2

task.wait(2)

local function setCameraBeforeAnimation(part)
	camera.CameraType = Enum.CameraType.Scriptable
	camera.CFrame = part.CFrame
	print("success")
end

setCameraBeforeAnimation(cameraTesting)

https://developer.roblox.com/en-us/api-reference/property/Camera/CameraSubject

Hmm it didn’t work, but you make an interesting point… is there a way I can halt the script until the camera has loaded in and then change it?

That’s weird, it works for me. Are you sure its a local script? Also, can you tell me where its located?

I don’t know if there is a way to do this.

It’s in StarterPlayerScripts, it CAN function there, right?

Edit: it did work, but is there a way I can do it without a few second delay, I need this to be the camera when the player joins.

You can try this:

repeat
    setCameraBeforeAnimation(cameraTesting) -- Calling your function 
until camera.CameraType == Enum.CameraType.Scriptable -- Calling it until cam is scriptable

I managed to do it by connecting it to a render stepped function, as so. Thanks as always guys for the help!

local RS = game:GetService("RunService")
local camera = workspace.CurrentCamera
local cameraPart = game.Workspace.CameraTest1

local function updateCameraCFRAME()
	camera.CameraType = Enum.CameraType.Scriptable
	camera.CFrame = cameraPart.CFrame
	camera.CameraSubject = cameraPart
end

local RsConnection = RS.RenderStepped:Connect(updateCameraCFRAME)
RsConnection:Disconnect()-- call this to disconnect function when needed then change camera back
1 Like