# Get Nearest Target

So I have this script that gets the nearest target but it doesn’t seem to get the nearest target. Am I doing something wrong here?

``````local npcs = workspace:WaitForChild("Npcs")
local currentTarget = nil

local rayCaycastParams = RaycastParams.new()
rayCaycastParams.FilterDescendantsInstances = {npcs}
rayCaycastParams.FilterType = Enum.RaycastFilterType.Whitelist
rayCaycastParams.IgnoreWater = true

local function castRay()
local nearestDistance = math.huge

for i,v in ipairs(npcs:GetChildren()) do
if v.PrimaryPart ~= nil then
local rayOrigin = character.PrimaryPart.Position
local rayDestination = v.PrimaryPart.Position
local rayDirection = (rayDestination - rayOrigin)
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, rayCaycastParams)

local human = v:FindFirstChildWhichIsA("Humanoid")

if raycastResult and human and human.Health > 0 then
local distance = math.floor(raycastResult.Distance)
if distance < nearestDistance and currentTarget == nil then
currentTarget = v
nearestDistance = distance
return true, raycastResult.Position, distance
end
end
end
end
currentTarget = nil
return false, nil, nil
end

local function findTarget()
local checkRay, position, distance = castRay()

if checkRay == true and currentTarget ~= nil then
-- stuff
end
end
``````

I’m a little confused, are you trying to get the nearest person in front of the player or around the player?

I want my NPC to get the nearest NPC. I want to get the nearest target around the NPC.

You should probably read what raycast is because it is essentially a straight line going in the direction you choose. You should use magnitude instead to get objects within a radius.

``````local MaxRadius = 10 -- Studs
local Position1 = Character.HumanoidRootPart.Position
local Position2 = NPC.HumanoidRootPart.Position

if (Position1 - Position2).Magnitude <= MaxRadius then
end
``````

What if there is a giant NPC? Would it do the same as raycasting?

raycast detects objects within a straight line. Magnitude detects objects within a circle(radius).

So this goes in a straight line?

``````local rayOrigin = character.PrimaryPart.Position
local rayDestination = v.PrimaryPart.Position
local rayDirection = (rayDestination - rayOrigin)
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, rayCaycastParams)
``````

yes.
you can use magnitude to check if position2 is within the radius of position1.

I fixed it with this:

``````local npcs = workspace:WaitForChild("Npcs")
local currentTarget = nil

local rayCaycastParams = RaycastParams.new()
rayCaycastParams.FilterDescendantsInstances = {npcs}
rayCaycastParams.FilterType = Enum.RaycastFilterType.Whitelist
rayCaycastParams.IgnoreWater = true

local function castRay()
local nearestDistance = math.huge

for i,v in ipairs(npcs:GetChildren()) do
local human = v:FindFirstChildWhichIsA("Humanoid")

if v.PrimaryPart ~= nil and human and human.Health > 0 then
local rayOrigin = character.PrimaryPart.Position
local rayDestination = v.PrimaryPart.Position
local rayDirection = (rayDestination - rayOrigin)
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, rayCaycastParams)

if raycastResult then
local distance = math.floor(raycastResult.Distance)
if distance < nearestDistance and currentTarget == nil then
currentTarget = v
nearestDistance = distance
return true, raycastResult.Position, distance
end
end
else
currentTarget = nil
end
end
currentTarget = nil
return false, nil, nil
end

local function findTarget()
local checkRay, position, distance = castRay()

if checkRay == true and currentTarget ~= nil then
-- stuff
end
end
``````
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.