Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make an NPC be able to detect you if your seen by them?

Basically Notoriety. Check out that game btw. When NPC look at you that detect you, but if you behind a wall, they don't detect players.

I tried making a hat for the NPCs that will touch a player if he/she is in the NPCs sight. But it goes through walls so that doesn't work.

Please help me. I have no idea what to do, iv been thinking about this for more than a year.

2 answers

✓ Accepted answer

you can use camera.GetPartsObscuringTarget()

more here

4 comments

0

So I tried NPC.Humanoid.Camera but it doesn't exist, how do use this information you gave me to make the NPC see other players?

0

you need to insert a camera object in the NPC, then set the camera subject property to the humanoid of the NPC, and i am sure that should work..

0

also btw, the reason why you can't access Humanoid.Camera is b/c it's not a property field of a Humanoid: here is a list of all properties of a humanoid: https://developer.roblox.com/en-us/api-reference/class/Humanoid

You can raycast from the NPC's head to the target, if it hits a body part belonging to them, then the NPC sees them.

local maximumDistance = 300 -- the ray's max distance, in studs
local ray = Ray.new(NPC.Head.Position, (TARGET.HumanoidRootPart.Position - NPC.Head.Position).Unit * maximumDistance)
local hit = workspace:FindPartOnRayWithIgnoreList(ray,{NPC})

if hit.Parent == target.Parent then
	print("i can see the player")
else
	print("i cannot see the player")
end

Keep in mind that I haven't tested the code above, so it may need some tweaking.

2 comments

0

Yes thank you, but although it does that, wouldn't a part raycast through itself.because if you make the raycast start at the heads position, that is inside the head, so wouldn't it also detect a player behind the NPC?also, would the Ray go through invisible or not collide parts??

0

No it wouldn't go through invisible or non-collideable parts, you can use the CollectionService to add tags to the parts which you want the ray to ignore, then simply add it to the IgnoreList table.

This discussion is preserved as it was. New answers, comments, and votes are closed.