Anyone know how to make a npc detect you when your in front of the npc?

im making a game like the original medal of honor game im doing the first mission one but when im behind the npc he detect me now yes i need it simple when you have a script tell me where to put it at anyway for those who don’t know you can research Medal of honor (1999) watch some gameplays if you don’t know what it is and you will see the game you can see when there behind the ai the ai doesn’t recognize the player btw when i join the npc looks back and shoots me

1 Like

please rephrase i can’t read this :sob:

1 Like

Try putting the script in a new part Infront the NPC, that’s the simple solution.


A warning of note, the tutorial is indeed 5 years old but hey, it gets the job done I think.

1 Like

Trying to make it suitable in all cases and must have removed counting only what is in front.

ServerScript
--ServerScript in ServerScriptService
local Players = game:GetService("Players")
local npcFolder = workspace:WaitForChild("NPCs")

local function inFront(npcRoot, playerRoot, fov, dist)
	local toPlayer = playerRoot.Position - npcRoot.Position
	if toPlayer.Magnitude > dist then return false end
	return math.deg(math.acos(npcRoot.CFrame.LookVector:Dot(toPlayer.Unit))) <= fov/2
end

local function detectPlayer(npc, player)
	local char = player.Character
	local root, npcRoot = char and char:FindFirstChild("HumanoidRootPart"), npc.PrimaryPart
	if root and npcRoot and inFront(npcRoot, root, 60, 20) then
		print(npc.Name.." detects "..player.Name)
	end
end

local function detectNearbyPlayers(npc)
	local humanoid = npc:FindFirstChild("Humanoid")
	if humanoid and humanoid.Health > 0 then
		for _, player in pairs(Players:GetPlayers()) do
			detectPlayer(npc, player)
		end
	end
end

local npcs = {}
for _, npc in pairs(npcFolder:GetChildren()) do
	local humanoid = npc:FindFirstChild("Humanoid")
	if humanoid then
		if not npc.PrimaryPart then npc.PrimaryPart = npc:FindFirstChild("HumanoidRootPart") end
		table.insert(npcs, npc)
		humanoid.Died:Connect(function()
			for i, v in pairs(npcs) do
				if v == npc then table.remove(npcs, i) break end
			end
		end)
	end
end

task.spawn(function()
	while #npcs > 0 do task.wait(1.2)
		for _, npc in pairs(npcs) do
			detectNearbyPlayers(npc)
		end
	end
end)
1 Like

edit: i fixed it select the npc ai script and ask roblox assistant this: make this script enable when a player is front of a model called German change the german to your npc btw make sure the ai script is enabled