Can you see check

What is the code to check if a player can see something?

For instance I have a lot of npc about with their names shown but I only want to show the NPC names that the player literally can see, not the ones behind walls etc.

Thank you for the assistance.

Example



local function DrawNPCInfo()
    for _, ent in pairs(ents.GetAll()) do 
        if ValidEntity(ent) && ent:IsNPC() then
             //code needed to check if I can see
        end
    end
end


[lua]function _R.Vector:IsInSight()
ply = LocalPlayer()
local trace = {}
trace.start = ply:EyePos()
trace.endpos = self
trace.mask = -1
local TheTrace = util.TraceLine(trace)
if TheTrace.Hit then
return false
else
return true
end
end[/lua]

Thank you, i’ll try this now. So giving it returns true or false means I can easily use this in an if statement.

Why dont you just

[lua]function _R.Vector:IsInSight()
ply = LocalPlayer()
local trace = {}
trace.start = ply:EyePos()
trace.endpos = self
trace.mask = -1
local TheTrace = util.TraceLine(trace)
return not TheTrace.Hit
end[/lua]