Advertisement
HowToRoblox

EnemyNPCHandler

May 18th, 2020
19,436
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.22 KB | None | 1 0
  1. local npc = script.Parent
  2. local hrpOfNPC = npc:WaitForChild("HumanoidRootPart")
  3.  
  4. local plrsHit = {}
  5.  
  6. local maxDistance = math.huge
  7.  
  8.  
  9. npc.Humanoid.Touched:Connect(function(touch)
  10.  
  11.     if game.Players:GetPlayerFromCharacter(touch.Parent) and not plrsHit[game.Players:GetPlayerFromCharacter(touch.Parent)] then
  12.  
  13.         plrsHit[game.Players:GetPlayerFromCharacter(touch.Parent)] = true
  14.        
  15.         touch.Parent.Humanoid:TakeDamage(20)
  16.        
  17.         wait(1)
  18.  
  19.         plrsHit[game.Players:GetPlayerFromCharacter(touch.Parent)] = false 
  20.     end
  21. end)
  22.  
  23.  
  24. while wait() do
  25.    
  26.     local plrs = game.Players:GetPlayers()
  27.    
  28.     local closestHRP
  29.    
  30.     for i, plr in pairs(plrs) do
  31.        
  32.         if plr.Character and plr.Character:FindFirstChild("HumanoidRootPart") and plr.Character.Humanoid.Health > 0 then
  33.            
  34.             local hrp = plr.Character.HumanoidRootPart
  35.            
  36.             local distanceBetween = (hrpOfNPC.Position - hrp.Position).Magnitude
  37.            
  38.            
  39.             if not closestHRP then closestHRP = hrp end
  40.            
  41.             if (hrpOfNPC.Position - closestHRP.Position).Magnitude < distanceBetween then
  42.                
  43.                 closestHRP = hrp
  44.                
  45.             end
  46.         end
  47.     end
  48.    
  49.     if closestHRP and (hrpOfNPC.Position - closestHRP.Position).Magnitude <= maxDistance then npc.Humanoid:MoveTo(closestHRP.Position) end
  50. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement