1 | local player = script.Parent.TargetPlayer.Value |
2 | while true do |
3 | wait() |
4 | script.Parent.Humanoid:MoveTo(player.Torso.Position) |
5 | end |
this script is a simple pathfind. i want this script to work with distance so if the player is a 20 distance away from the npc then the npc will follow the player if the player is not 12 or above distance away from the npc the npc will not follow him. how would i do that on this script?
Alright, see if this works.
We have to use ".magnitude", which gets a number value from two positions.
01 | local player = script.Parent.TargetPlayer.Value |
02 |
03 | distance = 20 |
04 |
05 | while true do |
06 | wait() |
07 |
08 | if (player.Torso.Position - (TheNPCsTorso).Position).magnitude < = distance then |
09 |
10 | script.Parent.Humanoid:MoveTo(player.Torso.Position) |
11 |
12 | end |
13 |
14 | end |
Change (TheNPCsTorso) to the pathway for your npc's torso and hopefully it will follow it.