CurrentCamera vs Camera

When looking for a client’s Camera object, developers should use this property rather than looking for a child of Workspace named ‘Camera’.

Why is it preferred to use current camera over workspace camera? the only scenario I can think of is if you have duplicate camera objects in the workspace but in what world would that ever happen accidentally? is there any other benefits to specifically using the current camera property over just directly indexing camera in workspace

2 Likes

Using a camera for viewport frames etc

Workspace.CurrentCamera is guaranteed to use the camera used by the local player

1 Like

Because CurrentCamera always points to the camera that’s set as your current one. Even though Roblox doesn’t allow multiple cameras in the workspace, that might change at some point, meaning you might have multiple instances named “Camera”.

3 Likes

a camera in viewpoint frame shouldnt be parented by workspace, I always parent them in the viewport frame itself

That is what you do, but anyone can have anything called camera in the workspace, it could be a model of a physical camera, you never know. Just use the property it is guaranteed to give the correct instance.

Even if it were possible to have multiple camera instances in workspace, there should never be a reason to have that, its a safe assumption to say that there will only ever be 1 camera in workspace unless a accident happens and if that accident were to happen it would be easily fixed

Forgot to factor in actual parts or models named Camera, that does make a valid point

I can give multiple reasons to have multiple cameras, starting with a payday 2 like CCTV camera system.

Directly indexing for children is bad practice anyways, it is not forwards compatible as you are assuming Roblox won’t introduce a member with the same name as the child. Remember that class members have higher precedence over children when indexing. Use :FindFirstChild and :WaitForChild instead. So the advice given in the documentation is valid.

I don’t quite understand the example you gave, you can just modify the existing camera or if you have a specific camera object at the very least it should be parented to a model or even have a different name?

Directly indexing in this case should be quite valid? when your using .CurrentCamera that just a property referencing the camera object of the player viewport, even if there was a chance for camera to be nil the property(value) would be nil aswell, so the same argument can be made about directly indexing a proprety for the camera reference

Instances can’t be nil. And not sure what you mean by everything else. workspace.CurrentCamera is safe, and it is never nil.

Well I assumed the reason you mention waitforchild/findfirstchild was because the camera object might be nil when I reference it? (might of misread so extra clarification would be nice if I did get this wrong)

Instances can be nil in a sense that they didn’t fully load in yet, for example Player.Character is also a proprety and the proprety can be nil if the character hasent fully been loaded in yet or hasent been set yet by roblox back-end stuff which is why people tend to do

Player.Character or Player.CharacterAdded:Wait()

Yes however that is intended that is why Roblox has an event for it. There is no “Workspace.CameraAdded” event.

workspace:GetPropertyChangedSignal(“CurrentCamera”):Wait()

I was stating that if camera was impossible to become nil, then there no harm in directly referencing it without .CurrentCamera, unless obviously there was multiple objects with the same name

There is, you are assuming Roblox won’t add in a member named Camera as a part of Workspace. Use :FindFirstChild if you are so keen on avoiding the property.

Alright, I was just wondering why people use .CurrentCamera over just directly getting it from workspace, its not like im avoiding i was just curious thanks for your input :stuck_out_tongue:

You can use Workspace.CurrentCamera is a property of the workspace and Workspace.Camera is an instance. Like both work, their is no one concrete method so there is no diffinitive method but Workspace.CurrentCamera is the more ideal method for finding the active camera.

This post answered the question perfectly F.Y.I.

7 Likes

that was pretty much my logic, “more ideal”

1 Like

Technically the main camera could be named something different than Camera, and there could also be more than one Camera (used for ViewportFrames). So you should definitely always use the CurrentCamera property if you’re trying to access the camera the player is using for the game viewport.