Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does this NPC not detect me in it's POV?

Asked by 7 years ago
Edited 7 years ago

I am making a script that will make it so if this NPC sees me it will run the function Pursue. Search() is on a loop, so it runs over and over again. Read the note blocks.

function Search()
    for i = 1, #subjects do
        if subjects[i]:IsA("Player") then
            print("FoundPlayer")
            subjectChar = (subjects[i].Character)
            local subjectHead = subjectChar:findFirstChild("Head")

            local vec = subjectHead.Position - viewerHead.Position
            local isInFOV = (vec:Dot(subjectHead.CFrame.lookVector) > 0)
            print("Checkpoint")--This prints
            if (isInFOV) and (vec.magnitude < maxSightRange) then
                print("IsInFOV")--This does NOT print, he can clearly see me.
                local ray = Ray.new(viewerHead.Position,vec.unit*1000)
                local por = fpor(workspace,ray,subjectChar,false)
                if por:IsDescendantOf(subjectChar) then
                    Pursue()
                end
            end
        end
    end
end
0
So is subjects a table of players? If so, it'd be better to just do game.Players:GetPlayers() directly, unless you're doing a lot of custom stuff. And use a for i, v in pairs(subject) do loop instead. GoldenPhysics 474 — 7y
0
Yes, and I have put off learning in pairs for a while now, I finally understand it! Thanks. iUnEarthly 85 — 7y

Answer this question