How can I stop a humanoid from getting too close to a player?
Asked by
8 years ago Edited 8 years ago
I have been working on a new project and wanted to incorporate pets or followers, However when I made them follow the players the pet kept walking into the player pushing the player. Is there a way I can make it so that they only follow the player if there is a big enough gap between the two? here is the script that I have been using
01 | local larm = script.Parent:FindFirstChild( "Left Arm" ) |
02 | local rarm = script.Parent:FindFirstChild( "Right Arm" ) |
04 | function findNearestTorso(pos) |
05 | local list = game.Workspace:children() |
13 | if (temp 2. className = = "Model" ) and (temp 2 ~ = script.Parent) then |
14 | temp = temp 2 :findFirstChild( "Torso" ) |
15 | human = temp 2 :findFirstChild( "Humanoid" ) |
16 | if (temp ~ = nil ) and (human ~ = nil ) and (human.Health > 0 ) then |
17 | if (temp.Position - pos).magnitude < dist then |
19 | dist = (temp.Position - pos).magnitude |
29 | local target = findNearestTorso(script.Parent.Torso.Position) |
31 | script.Parent.Humanoid:MoveTo(target.Position, target) |