ViewportFrame not working

I’m making a camera system. Everything worked just fine, but suddenly it just stopped working, here’s my script:

local UserInputService = game:GetService("UserInputService")

workspace.CurrentCamera.FieldOfView = 90

local frame = script.Parent

for i,v in pairs(workspace.SecurityCameras:GetChildren()) do
    if v:IsA("UnionOperation") or v:IsA("Part") or v:IsA("MeshPart") then
        local camera = Instance.new("Camera", v)
        camera.CameraType = Enum.CameraType.Scriptable
        camera.CameraSubject = v
        camera.CFrame = v.CFrame
        camera.Name = "CameraSecurity"
    end
end

frame.CurrentCamera = game.Workspace.SecurityCameras["1"].CameraSecurity
active_cam = game.Workspace.SecurityCameras["1"]

local cam = workspace.CurrentCamera

local folder = script.Parent.WorkspaceObjects
local rsFolder = game.ReplicatedStorage.CameraObjects

local function GetWorkspace() -- problem is probably somewhere here...
    
    local tabl = {}
    
    for i,v in pairs(workspace:GetDescendants()) do
        
        if v:IsA("UnionOperation") or v:IsA("Part") or v:IsA("MeshPart") then
            
            local success, Error = pcall(function()

                local _, visible = active_cam.CameraSecurity:WorldToScreenPoint(v.Position)

                if (visible) then
                    table.insert(tabl, i + 1, v)
                end
            end)
            
        end
        
    end
    
    return tabl
    
end

local function UseClone(tabl)
    for i,v in pairs(folder:GetChildren()) do

        v:Destroy()

    end
    for i,v in pairs(tabl) do

        v.Parent = folder

    end
end

function UpdateViewportFrame()
    
    local objects = GetWorkspace()
    
    for i,v in pairs(objects) do
        if v then
            local r = v:Clone()
            if r then
                r.Parent = rsFolder
            end
        end
    end
    
    UseClone(rsFolder:GetChildren())
    
end

spawn(function()
    while true do
    
        wait(1)
    
        UpdateViewportFrame()
    
    end
end)

-- ... to here

spawn(function() -- changes the camera. This is not it, definetly.
    while true do
        
        for i = 1, #workspace.SecurityCameras:GetChildren() do
            
            local input = UserInputService.InputBegan:Wait()
            
            if input.KeyCode == Enum.KeyCode.R then

                local success, Error = pcall(function()
                    active_cam = game.Workspace.SecurityCameras[i]
                    frame.CurrentCamera = active_cam.CameraSecurity
                end)

                if not success then
                    active_cam = game.Workspace.SecurityCameras.Camera1
                    frame.CurrentCamera = active_cam.CameraSecurity
                end
            end
            
        end
        
    end
end)

Help is appreciated! :slight_smile:

Nvm, i fixed it. It worked better with cam:WorldToViewportPoint(pos)

That really shouldn’t make much of a difference, it just ignores GuiInset, I’m assuming you CTRL+Z’ed a bunch of stuff.