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
Simply subtract a Vector value from it.
Say the target is at position (0,0,0). When you use this script, the npc walks to the location of the head of the closest player, which is (0,0,0). If you want him to stop 4 studs away, simply subtract 4 from the x or z value.
For example:
closestplayer.Head.Position+Vector3.new(-4,0,0) -- This gets the position of the head of the closest player and then subtracts 4 studs from the x value, so the npc will stop at this position instead.
I'm not sure exactly how you want it but you can play around with this concept of Vector adding to get the npc to stop where needed.