#How can I make this script detect NPCs?

1 messages · Page 1 of 1 (latest)

upper harbor
#

I've been trying for a while now to convert this script:
https://devforum.roblox.com/t/how-to-create-a-realistic-npc-eyesight-system/2456522
into a system that detects NPCs and not players.

Here is what I got so far before getting stumped:

local collectionService = game:GetService("CollectionService")
local TeamA = collectionService:GetTagged("TeamA")

local range = 650
local viewAngle = 70

local lastEnemySpotted

local function getNPCs()
local NPCs = {}
local getNPC = collectionService:GetTagged("NPCs")

for index, getNPCs in getNPCs do
    table.insert(NPCs, getNPC)
end

return NPCs

end

local function sightRaycast(TeamA, start, finish)
local raycastParams = RaycastParams.new()

raycastParams.FilterDescendantsInstances = {TeamA, getNPCs()}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude

local ray = workspace:Raycast(start, (finish - start), raycastParams)

if ray then
    return true
else
    return false
end

end

mellow delta
#

Also, use code blocks when sending code

pure sparrow
#

Would result in duplicates

#

All u need is npc = get tagged

#

And return it

upper harbor
#
local function getNPCs()
    local getNPC = collectionService:GetTagged("NPCs")
    return getNPC
end
upper harbor
upper harbor
#

also for the beginning of the "checkSight" function in the tutorial could I do

local function checkSight(TeamA)
    local spottable = {}
    
    local npcs = getNPCs()
    local spottableEnemies = collectionService:GetTagged("SpottableByA")
    
    for index, npcs in npcs do
        table.insert(spottable, npcs)
    end
    
    for index, spottableByA in spottableEnemies do
        table.insert(spottable, spottableByA)
    end 
    
    for index, spottableByA in spottable do
        if spottableByA:IsA("Model") then
            spottableByA = spottableByA.Torso
        end
    end