I have this that makes the npc walk to another npc but it just walks into it, it should walk to it but stop when it's 4 studs close to it how do I do this??
while wait() do
local distances = {}
local charwithdistance = {}
for number,char in pairs (game.Workspace.NPCs:GetChildren()) do
local distance = (char.Head.Position - script.Parent.Head.Position).magnitude
distances[number] = distance
charwithdistance[distance] = char
end
table.sort(distances)
for number2, plr in pairs (distances) do
local closestdistance = plr
local closestplayer = charwithdistance[(closestdistance)]
if closestplayer.Owner.Value ~= script.Parent.Owner.Value then
script.Parent.Humanoid:MoveTo(closestplayer.Head.Position)
break
end
end
end
I tryed doing this but it just broke the npc...
while wait() do
local distances = {}
local charwithdistance = {}
for number,char in pairs (game.Workspace.NPCs:GetChildren()) do
local distance = (char.Head.Position - script.Parent.Head.Position).magnitude
distances[number] = distance
charwithdistance[distance] = char
end
table.sort(distances)
for number2, plr in pairs (distances) do
local closestdistance = plr
local closestplayer = charwithdistance[(closestdistance)]
if closestplayer.Owner.Value ~= script.Parent.Owner.Value and closestdistance > script.Parent.GunStats.Range.Value then
script.Parent.Humanoid:MoveTo(CFrame.new(script.Parent.Head.Position, closestplayer.Head.Position) * CFrame.new(0,0, -25))
break
end
end
end

1 comment
Still doesn't work it just breaks the ai now it doesn't move at all. I updated my question with what I did pls help me ;(