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

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

Asked by 4 years ago

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

Log in to vote
1
Answered by 4 years ago

you can use camera.GetPartsObscuringTarget()

more here

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? AlexanderYar 788 — 4y
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.. User#23252 26 — 4y
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 User#23252 26 — 4y
Ad
Log in to vote
1
Answered by 4 years ago

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.

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?? AlexanderYar 788 — 4y
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. strongrussianboy123 68 — 4y

Answer this question