You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? I want an NPC to walk to another if the magnitude is under 100.
What is the issue? The NPC will not move.
What solutions have you tried so far? Asked for help in discord, no one answered
local zombieTorso = script.Parent.Torso
local zombieHumanoid = script.Parent.Humanoid
local function findTarget()
local aggroDistance = 100
local Target = nil
for i, v in pairs(game.Workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local torso = v:FindFirstChild("Torso")
if human and torso and v ~= script.Parent then
if (zombieTorso.Position - torso.Position).magnitude < aggroDistance then
aggroDistance = (zombieTorso.Position - torso.Position).magnitude
Target = torso
end
end
end
end
while wait(1) do
local torso = findTarget()
if torso then
zombieHumanoid:MoveTo(torso.Position)
end
end```
local zombieTorso = script.Parent.Torso
local zombieHumanoid = script.Parent.Humanoid
local function findTarget()
local aggroDistance = 100
local Target = nil
for i, v in pairs(game.Workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local torso = v:FindFirstChild("Torso")
if human and torso and v ~= script.Parent then
if (zombieTorso.Position - torso.Position).magnitude < aggroDistance then
aggroDistance = (zombieTorso.Position - torso.Position).magnitude
Target = torso
end
end
end
return Target
end
while wait(1) do
local torso = findTarget()
if torso then
zombieHumanoid:MoveTo(torso.Position)
end
end
This implementation will only work for R6, as you’re checking for ‘Torso’ when you iterate through the descendants of the workspace.
The FindTarget method doesn’t return anything, so ‘torso’ in the while loop below will always be nil, hence the if condition below won’t ever be true.
Here’s a re-work of the script, it’ll work for R6 and R15 too.
local zombieTorso = script.Parent.Torso -- Assuming this is an R6 rig.
local zombieHumanoid = script.Parent.Humanoid
local function findTarget()
local aggroDistance = 100
local Target = nil
for i, v in pairs(game.Workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local torso
-- We can't assume the player will have an R6 rig (unless you've set the game settings to be otherwise). So we'll check to be on the safe side.
if human.RigType == Enum.HumanoidRigType.R6 then
torso = v:FindFirstChild("Torso")
elseif human.RigType == Enum.HumanoidRigType.R15 then
torso = v:FindFirstChild("HumanoidRootPart")
end
if human and torso and v ~= script.Parent then
if (zombieTorso.Position - torso.Position).magnitude < aggroDistance then
aggroDistance = (zombieTorso.Position - torso.Position).magnitude
Target = torso
end
end
end
return Target
end
while wait(1) do
local torso = findTarget()
if torso then
zombieHumanoid:MoveTo(torso.Position)
end
end
Actually, since R6 characters AND, R15 characters have a HumanoidRootPart, you can just use that without an if statement.
local zombieTorso = script.Parent.Torso
local zombieHumanoid = script.Parent.Humanoid
local function findTarget()
local aggroDistance = 100
local Target = nil
for i, v in pairs(game.Workspace:GetChildren()) do
local human = v:FindFirstChildWhichIsA("Humanoid")
local torso = v:FindFirstChild("HumanoidRootPart")
if human and torso and v ~= script.Parent then
if (zombieTorso.Position - torso.Position).Magnitude < aggroDistance then
aggroDistance = (zombieTorso.Position - torso.Position).Magnitude
Target = torso
end
end
end
return Target
end
while task.wait(1) do
local torso = findTarget()
if torso then
zombieHumanoid:MoveTo(torso.Position)
end
end
Plus, you only check if human exists after you do the rig type check, meaning you may get an error thrown.