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

2 comments

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.

0

Yes, and I have put off learning in pairs for a while now, I finally understand it! Thanks.