Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

Detect if player is seen?

Asked by 9 years ago

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

2
Anything is possible in the rbx world. TrollD3 105 — 9y
0
I dont rhink that is possible, without lots of positioning and ifs for camera ScriptFusion 210 — 9y
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. Vezious 310 — 9y

1 answer

Log in to vote
0
Answered by
4Bros 550 Moderation Voter
9 years ago

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
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. EzraNehemiah_TF2 3552 — 9y
0
he asked if it was possible, i gave him an example so he could understand how it would work 4Bros 550 — 9y
Ad

Answer this question