How to change the Camera View in Script?

Hello guys,
I'm new in Core and in game development.

I want to make the player's camera switch to another camera that will be the view of the cannon, in the moment it interacts with the Trigger, and in addition, the cannon moves in the direction the player is looking at. But my code doesn't work, there's no error, but nothing happens when I interact with Trigger

Sorry about my code, i'm still learning.

local canhao = script.parent.parent

local camera = script:GetCustomProperty("Camera"):WaitForObject()
local canhaoTrigger = canhao:FindChildByName("CanhaoTrigger")
local canhaoTriggerPosition = canhaoTrigger:GetPosition()

local LOCAL_PLAYER = Game.GetLocalPlayer()



function onTriggerInteraction()


LOCAL_PLAYER:SetOverrideCamera(camera)

end

canhaoTrigger.interactedEvent:Connect(onTriggerInteraction)

Sem título

You would need to use "FindDescendantByName" instead of "FindChildByName" because your camera is not a direct child of the canhao though it is a descendant of the canhao object. For example:

 local canhao = script.parent.parent

local camera = script:GetCustomProperty("Camera"):WaitForObject()
local canhaoTrigger = canhao:FindDescendantByName("CanhaoTrigger")
local canhaoTriggerPosition = canhaoTrigger:GetPosition()

local LOCAL_PLAYER = Game.GetLocalPlayer()



function onTriggerInteraction()


LOCAL_PLAYER:SetOverrideCamera(camera)

end

canhaoTrigger.interactedEvent:Connect(onTriggerInteraction)

It looks to me that the CanhaoTrigger is a direct child of the canhao object. Canhou is script.parent.parent, and the trigger is in script.parent.parent.CanhaoTrigger (it has less indent than the other members of the ClientContext).

I'm not seeing the error here though tbh, so maybe I'm misunderstanding the proposed solution. If I had to guess though, I'd say it's probably a networking issue. The CanhouTrigger object might not be visible to the script because the script is operating within a client context and the Canhao Trigger is visible within the default context. Maybe you could try setting the Canhao Trigger to be networked? (Note: I might not have the experience in core to back this up, but if I had to guess this is what it looks to be. Changing the API used to FindDescendant might also further "bulletproof" your code in case you ever have to change the hierarchy.)

Hello guys, thanks for the help.
I changed my code and now its changing the camera, but not to the CannonCamera.
What's happening?

local canhao = script.parent.parent.parent
local canhaoTrigger = canhao:FindDescendantByName("CanhaoTrigger")
local canhaoTriggerPosition = canhaoTrigger:GetPosition()
local player = Game.GetLocalPlayer()



function onTriggerInteraction()

local propCamera = script:GetCustomProperty("CanhaoCamera")


local defaultCamera = World.SpawnAsset(propCamera,
    { position = Vector3.New(260.584, -105.350, 137.686) })


player:SetOverrideCamera(defaultCamera,10)

Task.Wait(2)

end

canhaoTrigger.interactedEvent:Connect(onTriggerInteraction)

bloggif_609311d1c20d6

Sem título

So there are two possible issues in the script you posted above. One, is that you may not have added a core object reference custom property named "CanhaoCamera" (don't include the quotes) to your script. The second issue is that you need to call "script:GetCustomProperty("CanhaoCamera"):GetObject()" or "script:GetCustomProperty("CanhaoCamera"):WaitForObject()" to actually get the core object that is referenced in the core object reference custom property. For example:

This video does a great job of explaining how core object reference custom properties work.