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 5 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:

local SearchDistance = 10000

local NPC = script.Parent
local HRP = NPC.HumanoidRootPart
local Humanoid = NPC:FindFirstChildOfClass("Humanoid")

local function getNearestPlayer()
    local playerDistances = {}
    if HRP then 
        for i, player in pairs(game.Players:GetPlayers()) do 
            -- get the distance between the player's character and the HRP's position
            local distance = player:DistanceFromCharacter(HRP.Position)
            -- 'table.insert(x, y, z)' has 3 arguments. x = table(array), y = number position (optional), z = variable/object/instance your adding to the table
            table.insert(
                playerDistances,
                {
                    player,
                    distance
                }
            )
        end
    end
    if #playerDistances >= 1 then 
        --'table.sort(x, y)' has 2 arguments. x = table(array), y = function (optional)
        table.sort(
            playerDistances,
            function(a, b) -- 'a' and 'b' is the data/table inserted into the 'playerDistances' table
                -- we are comparing the distances and ordering them from least to greatest
                -- so we get the data's second value 'a[2]' and 'b[2]' which is the distances
                return a[2] < b[2]
            end
        )
        for i, dataTable in pairs(playerDistances) do  
            local player = dataTable[1]
            local distance = dataTable[2]
            if player and distance then 
                -- 'i' is the number position of the 'dataTable'
                if i == 1 and distance < SearchDistance then 
                    return player
                end
            end
        end
    end
end

if HRP and Humanoid.Health > 0 then
    wait(.5)
    local nearestPlayer = getNearestPlayer()
    if nearestPlayer and nearestPlayer.Character.Masked.Value == true then 
game.Workspace.guard.sight.Handle.Touched:Connect(function(c)
print("detected")
script.Parent.Torso.Anchored = false
script:Destroy()
    end)
end
end

pay attention to

if HRP and Humanoid.Health > 0 then
    wait(.5)
    local nearestPlayer = getNearestPlayer()
    if nearestPlayer and nearestPlayer.Character.Masked.Value == true then 
game.Workspace.guard.sight.Handle.Touched:Connect(function(c)
print("detected")
script.Parent.Torso.Anchored = false
script:Destroy()
    end)
end
end

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 — 5y
0
just an fyi, instead of an invisible part, you may want to use raycasting. Mr_Pure 129 — 5y
0
you can use raycasting hellmatic 1523 — 5y

1 answer

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

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 — 5y
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 — 5y
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 — 5y
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 — 5y
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 — 5y
0
dont care CommanderCaubunsia 126 — 5y
Ad

Answer this question