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

3 comments
Try using tostring(nearestplayer)
just an fyi, instead of an invisible part, you may want to use raycasting.
you can use raycasting