Getting all players in camera view

I am trying to get all visible players in the characters camera I have been using WorldToScreenPoint but it is very unreliable are there any alternitives that actually get all the players in view?

1 Like

Set the Camera | Roblox Creator Documentation of the character humanoid

How does that help I am trying to get all visible players but WorldToScreenPoint is including players behind walls.

Easiest way would be to iterate over all the players in the game on the local player and check if the current iteration player’s HumanoidRootPart is within the fov of the camera. I’m just taking a code snippet from my LiveCameraFeed script. Make sure to skip the local player though.

function partInFieldOfVision(part)
	local vector = (part.CFrame.Position - cam.CFrame.Position) -- cam is the current camera, this gets the vector between the part and the camera
	local magnitude = vector.Magnitude -- Magnitude is the distance
	if magnitude > CameraRange then return false end -- CameraRange is just a number.
	local dir = vector.unit -- Unit basically keeps the direction, but sets the length to 1
	local angle = math.deg(math.acos(cam.CFrame.LookVector:Dot(dir))) -- This will get the angle, in degrees of the camera to the part
	return angle <= cam.FieldOfView -- See if the angle is within the camera's field of view
end

After you get whether or not they’re within field of view, you can raycast from the camera to the seen players humanoid root part to see if there’s any wall blocking the way (I would add the local player’s character to an ignore list though). Here’s a simple example:

1 Like

You could use the GetPartsObscuringTarget function of the Camera

Thanks for the help I am using your way but it is causing issues with lookAt do you know why this is or can you help?

Can you try explaining this more?

Basically it’s a Lock it gets the nearest player to the mouse, it works fine but when I use GetPartsObscuringTarget it makes the Camera spin wildly and into locks onto the Target for a couple seconds before unlocking and repeat. I do not know why this is happening as when I use WorldToScreenPoint to check if the player is visible it does not do this. I am wondering why it is doing this and how to stop it.