Viewport Frame for Camera System in Roblox Studio not Working

87 Views Asked by At

I've been working on a roblox game based on Five Nights at Freddy's 1. I decided to go with an approach to have a minimap on the desk in front of you, with buttons next to each camera on the map. When a button is pressed, it runs a script that triggers a remote event to all clients, saying the button has been pressed, along with the name of the camera as an argument.

On every client, after recieving this message, this function is ran:

local function onCameraEvent(cameraName)
    print("Clicked")
    for _, child in game.StarterGui.CamerasUI.Cameras:GetChildren() do
        if (child.Name ~= cameraName) then
            child.Visible = false 

            for _, descendant in pairs(child:GetDescendants()) do
                descendant:Destroy()
            end

        end
    end

    game.StarterGui.CamerasUI.Cameras[cameraName].Visible = true 


    local cam = Instance.new("Camera", game.StarterGui.CamerasUI.Cameras[cameraName])
    cam.Name = "Cam"
    cam.CameraType = Enum.CameraType.Scriptable
    cam.FieldOfView = 70

    game.StarterGui.CamerasUI.Cameras[cameraName].CurrentCamera = cam
    
    
    local model = Instance.new("WorldModel", game.StarterGui.CamerasUI.Cameras[cameraName])
    model.Name = "World Model"
    


    for i, minimapObjects in pairs(game.Workspace.Map:GetChildren()) do
        if minimapObjects:IsA("Sound") or minimapObjects:IsA("SoundGroup") or minimapObjects:IsA("SoundEffect") then return end
        minimapObjects:Clone().Parent = model
    end
    local camPart = game.Workspace.Cameras:WaitForChild(cameraName)


    game["Run Service"].RenderStepped:Connect(function()
        local camFr = game.Workspace.Cameras[cameraName].CFrame
        cam.CFrame = camFr
    end)
    
end

Essentially, it finds the SurfaceUI within the starterUI, then enables the viewport frame for the camera who's button was clicked. Also disabling any other viewports that were enabled previously. Then it adds a camera object and a copy of the map to the viewport frame. I did make sure that the viewport frame's "CurrentCamera" property is linked to the newly created camera.

This creates a copy of the map within the viewport frame, with its camera in the same position as the real physical one would be. The goal is, for the viewport frame to just be an image of what the map looks like from the perspective of whichever camera you clicked on.

The UI tree looks like this:

Starter UI > Surface GUI > Folder > Viewport Frame > Camera & Map Model

(ViewportFrame.CurrentCamera = the camera it parents)

(SurfaceGUI.adornee = Workspace.randomPartToDisplayCamera)

Final Question

After completing all this code, everything looks as if it should function correctly. For example, when I click a button, the correct viewport frame is activated, the map and camera is added to it, and everything looks fine. But, for some reason, no matter what I do, I just can't get the viewport frame to show up on the surface UI. I've tried going and making a separate frame with just one part in it and it works fine, but this just won't show up. Can anyone help me figure this out?

0

There are 0 best solutions below