local function checkSight(npc)
local head = npc:FindFirstChild("Head")
if not head then return nil end
local detectable = {}
for _, character in ipairs(getCharacters()) do
table.insert(detectable, character)
end
for _, object in ipairs(CollectionService:GetTagged("Detectable")) do
table.insert(detectable, object)
end
local detected = {}
local headPosition = head.Position
local headCFrame = head.CFrame
local lookVector = headCFrame.LookVector
for _, object in ipairs(detectable) do
if object:IsA("Model") then
object = object.PrimaryPart
if not object then continue end
end
local objectPosition = object.Position
local direction = (objectPosition - headPosition).Unit
local dotProduct = direction:Dot(lookVector)
local angle = math.deg(math.acos(dotProduct))
local distance = (headPosition - objectPosition).Magnitude
if angle <= fieldOfView and distance <= viewRange then
if not raycast(npc, headPosition, objectPosition) then
table.insert(detected, object)
end
end
end
return #detected > 0 and detected or nil
end