#Script not able to detect objects

1 messages · Page 1 of 1 (latest)

polar sleet
#

So I have been tweaking a script from the Dev Forums with some help and it is supposed to detect any model with the tag "Detectable" but it doesn't. I get no errors yet it still does not work. It can detect players correctly but not models.

The part of the script that I believe to be causing the issue is the checkSight function:

cloud groveBOT
#

studio** You are now Level 5! **studio

polar sleet
#
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
brave haven
#

probably didnt put the tag on the models or wrong spelling

polar sleet
#

They are all tagged

#

and spelling is correct

brave haven
#

add prints make sure code is running etc

#

take debugging steps

polar sleet
#

Also is there anything wrong with the script its self

polar sleet
#

Everything seems to be running fine

#

I added debuging print statements

#

Added some more

#

Yeah all the functions run

#

so im not sure why it won't detect objects

outer grotto
polar sleet
#

thats gonna help more

#

I have to go for a while now I will add it in later

polar sleet
outer grotto
polar sleet
#

I'd assume

polar sleet
#
    if not npc:IsA("Model") then
        continue
    end

    if not humanoid then
        continue
    end
#

lemme post the full script

outer grotto
polar sleet
#

though now I use ipairs in getCharacters so now it tells me how many models are added

#

which is 2

outer grotto
# polar sleet which is 2

Okay, so it reaches checkSight, yeah? Like if you print out in that function who the NPC is, does it make sense?

outer grotto
# polar sleet yeah it prints

Okay, so how about the raycast? Does it get there?

How about the direction values? field of view?

How do the raycast parameters look before you call the function?

polar sleet
#

Also I visualized the ray

#

lemme send a screenshot

outer grotto
polar sleet
outer grotto
polar sleet
#

Yeah I messed it up in some way

#

I need to define the variables for it earlier

#

to get an accurate picture

#

but this does show that the rays are firing

#

It appears it fires 1 ray per detectable thing in the table

#

as when I joined it fired 3

polar sleet
cloud groveBOT
#

studio** You are now Level 6! **studio