How to get all players within view

Hello! I am trying to make a spotting system, similar to Phantom Forces. I need to figure out where, when the function is ran, it will get all of the players within field of view of the camera. Note: I want it to also detect players behind walls as well.

local function getPlayersWithinFOV(cameraCF)
--Code
	return tableOfPlayers
end

By the way, thank you so much for your time :slight_smile:

The WorldToScreenPoint function of the Camera instance can tell you if a Vector3 position is in view on the screen. It returns 2 variables, a Vector3 whose X and Y components are the 2d position on the screen in pixels, and a boolean that is true if the point is visible in the viewport.

So your function could be like:

local function getPlayersWithinFOV()
    local playerlist = {}
    for _, player in ipairs(game.Players:GetPlayers()) do
        local head = player.Character and player.Character:FindFirstChild("Head")
        if (head) then
            -- ignore the position value
            local _, visible = workspace.CurrentCamera:WorldToScreenPoint(head.Position)
            if (visible) then
                table.insert(playerlist, player)
            end
        end
    end
    return playerlist
end
8 Likes

Thank you so much man! Very appreciated