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

How can I detect whether a player can see an object or not?

Is there any possible way to know whether a part is within your line of sight? I have looked around in the Wiki, and I haven't found anything of the sort... So if there is any way to create a method that can detect objects within your sight (detect whether the player can see the object or not), that'd be nice to know! Thanks in advance!

1 answer

Here I'll help u out

Btw this was done by IPad so sorry if there were any errors or simple mistakes

Make a local script put In startergui

local Player = game.Playera.LocalPlayer
local Camera = Workspace.CurrentCamera
local FOV = Camera.FieldOfView -- FOV = FieldOfView
local Parts = {} -- leave empty if u don't know what u are doing

Scan = function(object)
  for i, v in pairs(object:GetChildren()) do
   table.insert(Parts, v)
   if #v:GetChildren() > 0 then
    Scan(v) -- scan the object since it has children
  end
 end
end

Scan(Workspace) -- this will scan everything in the workspace

function getNearestObject()
   for i, v in pairs(Parts) do
      if (Player.Character.Torso.Position - v.Position).magnitude < FOV then -- magnitude gets the distance between the 2 points
        return v
      end
   end
end

while wait() do -- continuos loop
   local object = getNearestObject()
   print(object.Name) -- objects name goes towards output
end

2 comments

0

This doesn't work, and I don't see how it should work. The if statement that checks whether the distance between the player and the object is greater than your FOV wont do what I asked for in my question. That statement would return true even if objects were behind me. I want to have objects that you can SEE, not that are in a certain range from you. (The game is always in first person state, by t

0

Oops sorry change the greater to a less than

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