HowToRoblox

FollowScript

Sep 25th, 2019
14,507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.86 KB | None | 0 0
  1. function findNearestPlayer(Position)
  2.     wait(0.3)
  3.     local List = game.Workspace:children()
  4.     local Torso = nil
  5.     local Distance = 30
  6.     local Temp = nil
  7.     local Human = nil
  8.     local Temp2 = nil
  9.     for x = 1, #List do
  10.         Temp2 = List[x]
  11.         if (Temp2.className == "Model") and (Temp2 ~= script.Parent) then
  12.             Temp = Temp2:findFirstChild("HumanoidRootPart")
  13.             Human = Temp2:findFirstChild("Humanoid")
  14.             if (Temp ~= nil) and (Human ~= nil) and (Human.Health > 0) then
  15.                 if (Temp.Position - Position).magnitude < Distance then
  16.                     Torso = Temp
  17.                     Distance = (Temp.Position - Position).magnitude
  18.                 end
  19.             end
  20.         end
  21.     end
  22.     return Torso
  23. end
  24.  
  25.  
  26.  
  27.  
  28. while true do
  29.     local target = findNearestPlayer(script.Parent.HumanoidRootPart.Position)
  30.     if target ~= nil then
  31.         script.Parent.Humanoid:MoveTo(target.Position, target)
  32.     end
  33. end
Add Comment
Please, Sign In to add comment