Camera script won't work

Hi all!
I am working on a camera script and i have a part called “Cam” where the camera should be. When i try to run everything it says that “Cam is not a part of Workspace.studio”, even though it is. What’s the problem here?

local camera = game.Workspace.Camera
local studio = game.Workspace.Studio

game.ReplicatedStorage.HatchEgg.OnClientEvent:Connect(function(pet)
	camera.CameraType = Enum.CameraType.Scriptable
	camera.CFrame = studio.Cam.CFrame

cam

1 Like

Replace it with

local camera = workspace.CurrentCamera

Nope, the script still says the same thing :confused:

How about

local camera = workspace:WaitForChild("Camera")

It now says that there’s an infinite yield possible for that line.

Oh, my bad, I edited my reply, it should work now

If this script is run before the game fully loads, workspace.Studio may not exist at that point in the code due to a race condition.

To fix this, you could try replacing local studio = game.Workspace.Studio with local studio = workspace:WaitForChild("Studio") so the game will wait for that object to load (see Instance:WaitForChild)

If that doesn’t work, try printing studio to see if you are referencing the right object.

Everything’s still the same. I’m starting to think that there’s something wrong with the line with cam, and not studio

Try

local camera = workspace:WaitForChild("Camera",1)

If that does not work then try

local camera = game:WaitForChild("Studio",1)

instead of

local camera = game.Workspace.Studio

add a wait for 3 seconds on the first line and make sure the script is a local script

local Camera = workspace.CurrentCamera
local Studio = workspace.Studio

game.ReplicatedStorage.HatchEgg.OnClientEvent:Connect(function(pet)
wait()
Camera.CFrame = Studio.Cam.CFrame

try with this

local Workspace = game:GetService("Workspace");
local camera = Workspace:FindFirstChild("CurrentCamera")
local studio = Workspace.Studio

local TweenService = game:GetService("TweenService");
local tInfo = TweenInfo.new(0);


game.ReplicatedStorage.HatchEgg.OnClientEvent:Connect(function(pet)
local goal = {CFrame = studio.Cam.CFrame;}
local tween = TweenService:Create(camera,tInfo,goal)

	camera.CameraType = Enum.CameraType.Scriptable
	tween:Play()
end)

if it doesn’t work, change a part of the code with OnServerEvent
And if it still doesn’t work, I don’t know what other solution to give you :sweat_smile:

Using repeat wait() generally should be avoided as it is inefficient and usually unnecessary because of better alternatives.

In this case,

repeat wait()
	Camera.CameraType = Enum.CameraType.Scriptable
until Camera.CameraType == Enum.CameraType.Scriptable

would be exactly the same as

wait()
Camera.CameraType = Enum.CameraType.Scriptable

because the code waits, sets the camera type to scriptable, then checks if it is scriptable before continuing the code.

1 Like