How would I go about detecting when an NPC "sees" a player?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Make an NPC “see and detect” a player, and get their username.

  2. What is the issue? Include screenshots / videos if possible!
    I’m out of ideas on how to do something like this

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried making a part for the npc’s vision but then the npc can see you through walls.
    I also tried searching on YouTube for tutorials and didn’t find anything

If anyone can let me know of a service or a technique of making detection like Notoriety or Assassin’s Creed I appreciate it! I do have an already made way of a restricted area, for detecting if the npc should attack or not

hey! i recently made a detection system. although this uses parts and not players, you can refactor this code to work with players and NPCs.

local LookPart = workspace.Look
local Part = workspace.Part

while task.wait() do
	local Dot = (LookPart.Position - Part.Position).Unit:Dot(LookPart.CFrame.LookVector)
	if Dot > 0.7 then
		local Params = RaycastParams.new()
		Params.FilterType = Enum.RaycastFilterType.Exclude
		Params:AddToFilter({LookPart,Part})
		local Raycast = workspace:Raycast(LookPart.Position,-(LookPart.Position - Part.Position).Unit * (LookPart.Position - Part.Position).Magnitude,Params)
		if not Raycast then
			print("Found")
		end
	end
end

if you have any questions, ask away

What do you mean by uses parts and not players? Does it not detect players?

basically, the “lookpart” is the part that detects another specific part. it doesn’t work with players, but you can definitely use this code to work for players with a few adjustments.

So Part is the part it will detect? So I change that to HumanoidRootPart?

i changed the code for you to hopefully work for players.

local LookPart = workspace.Look

while task.wait() do
	for _, Player in pairs(game.Players:GetPlayers()) do
		local Character = Player.Character or Player.CharacterAdded:Wait()
		local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
		local Dot = (LookPart.Position - HumanoidRootPart.Position).Unit:Dot(LookPart.CFrame.LookVector)
		if Dot > 0.7 then
			local Params = RaycastParams.new()
			Params.FilterType = Enum.RaycastFilterType.Exclude
			Params:AddToFilter({LookPart,Character:GetChildren()})
			local Raycast = workspace:Raycast(LookPart.Position,-(LookPart.Position - HumanoidRootPart.Position).Unit * (LookPart.Position - HumanoidRootPart.Position).Magnitude,Params)
			if not Raycast then
				print("Found")
			end
		end
	end
end

let me know if this works

It works, just had to change the LookPart to script.Parent.Detector. Thanks for sharing this with me!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.