Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Detect if player is seen?

Question Is it possible to detect if one player has another player is in there view?
Thanks

3 comments

2

Anything is possible in the rbx world.

0

I dont rhink that is possible, without lots of positioning and ifs for camera

0

It's in that slender game. So it's possible. Problem is, I don't know how to do it, and a lot of others don't either.

1 answer

✓ Accepted answer

This script below will tell you all players in the client's view. This is done by using the :WorldToScreenPoint(Vector3) method of the camera.

---LOCAL SCRIPT---
local Players = game:GetService'Players'
local RunService = game:GetService'RunService'

repeat RunService.RenderStepped:wait() until Players.LocalPlayer and Players.LocalPlayer.Character and workspace.CurrentCamera -- Wait until the stuff we need is here

local camera = workspace.CurrentCamera
local player = Players.LocalPlayer
local character = player.Character

function getPlayers()
	local tab = {}
	for i,v in next,Players:GetPlayers() do -- :GetPlayers() returns a table of all Players and only players
		if v ~= player then -- If the player isn't equal to the client's player
			table.insert(tab,v)
		end
	end
	return tab
end

function getPlayersInView(t)
	local tab = {} -- Create the table
	for i,v in next,t do
		if v and v:IsA("Player") and v.Character then -- Check if the value is a player and has a character
			local torso = v.Character:FindFirstChild'Torso'
			if torso then -- Check for a torso
				local screenCords,IsInView = camera:WorldToScreenPoint(torso.Position) -- Returns coordinates of the position mapped to screen coordinates along with a bool if the position is visible on the screen
				if IsInView then
					table.insert(tab,v) -- Add the value to the table
				end
			end
		end
	end
	return tab -- Return the table
end


while wait(2) do -- Only runs every 2 seconds
	local playersInView = getPlayersInView(getPlayers())
	if #playersInView >= 1 then -- If the players in the camera's view are greater than or equal to 1
		for i,v in next,playersInView do
			print(v.Name)
		end
	else
		print("No players in view")
	end
end

2 comments

1

Dude, you know that... This was a request question and you shouldn't answer it like that, all he's asking is if it's possible. You can't give him a script if he doesn't provide one for you.

0

he asked if it was possible, i gave him an example so he could understand how it would work

This discussion is preserved as it was. New answers, comments, and votes are closed.