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

How to make an NPC do something when a character walks IN FRONT of it?

Asked by 6 years ago

Im trying to remake a Notoriety guard, and ALL i need is a way to find out how to print "Detected" (easy) but when a player or the character i mean walkes IN FORNT of it? I tried making an accesory with part that is transparent, (now its not becasue im testing) and this script didnt work:

01local SearchDistance = 10000
02 
03local NPC = script.Parent
04local HRP = NPC.HumanoidRootPart
05local Humanoid = NPC:FindFirstChildOfClass("Humanoid")
06 
07local function getNearestPlayer()
08    local playerDistances = {}
09    if HRP then
10        for i, player in pairs(game.Players:GetPlayers()) do
11            -- get the distance between the player's character and the HRP's position
12            local distance = player:DistanceFromCharacter(HRP.Position)
13            -- 'table.insert(x, y, z)' has 3 arguments. x = table(array), y = number position (optional), z = variable/object/instance your adding to the table
14            table.insert(
15                playerDistances,
View all 56 lines...

pay attention to

01if HRP and Humanoid.Health > 0 then
02    wait(.5)
03    local nearestPlayer = getNearestPlayer()
04    if nearestPlayer and nearestPlayer.Character.Masked.Value == true then
05game.Workspace.guard.sight.Handle.Touched:Connect(function(c)
06print("detected")
07script.Parent.Torso.Anchored = false
08script:Destroy()
09    end)
10end
11end

it doesnt print anything, i tried script.Parent which is the actual model /NPC, it say parent is NIL!?!?!???

please help

0
Try using tostring(nearestplayer) retrobricks 162 — 6y
0
just an fyi, instead of an invisible part, you may want to use raycasting. Mr_Pure 129 — 6y
0
you can use raycasting hellmatic 1523 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
01if HRP and Humanoid.Health > 0 then
02    wait(.5)
03    local nearestPlayer = getNearestPlayer()
04Local np = tostring(nearestPlayer)
05    if np and np.Character.Masked.Value == true then
06game.Workspace.guard.sight.Handle.Touched:Connect(function(c)
07print("detected")
08script.Parent.Torso.Anchored = false
09script:Destroy()
10    end)
11end
12end

I did this on mobile so my capitalisations may be wrong!

0
no but the script.Parent is the NPC and not the player. im not having trouble with player that clicks it. CommanderCaubunsia 126 — 6y
0
Your question was to fix the printing, so I answered it. It’s becoming a nil value because your referencing a function not a number, string or Boolean, retrobricks 162 — 6y
0
im not talking about that though, i even tested it and it obviously makes no difference, why did i eve test it, because your changing somehting that relates to the character touching it, not the NPC!!!!!! CommanderCaubunsia 126 — 6y
0
Ok lets focus on making a NEW script, how did Brick_man manage to make a script that makes NPC act like your detected (i dont need that part yet) when you walk in FRONT of them and not just touch them? CommanderCaubunsia 126 — 6y
View all comments (2 more)
0
It says in the question why it isn’t printing, over than that it looks like I cannot help you retrobricks 162 — 6y
0
dont care CommanderCaubunsia 126 — 6y
Ad

Answer this question