Advertisement
Marcel12311

Roblox (Small AI PART/NPC)Script

Mar 28th, 2022
1,749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.93 KB | None | 0 0
  1. --[[
  2. --------------------------------
  3. INSTRUCTION:
  4. 1.create Script inside part in workspace
  5. 2.put this code in script
  6. 3.run!
  7. --------------------------------
  8.  
  9.  
  10. SOCIAL/ADS:
  11. --------------------------------
  12. my YouTube channel:
  13. https://www.youtube.com/channel/UC9mjJnPdd0aUqc_sHF2U_Tg
  14. my profile on Roblox:
  15. https://www.roblox.com/users/140427102/profile
  16. --------------------------------
  17.  
  18. Enjoy guys!
  19. --]]
  20. -- Variables settings
  21. local klocek = script.Parent -- change path of part in Workspace
  22. local max_distance = 30 -- max distance of range part to see humanoid(player) object
  23. local speed = 0.33 -- change it (what speed of functions are to be performed)
  24. local speedPart = 12 -- speed of part
  25. local DamagePart = 6 -- damage of part
  26. local startedPosition = klocek.Position -- variable storage first position of part so we can returned the started postion later
  27. local isAttacking = false -- if part attacking player so does not attack other opponents
  28. --------------------------
  29.  
  30. local function ReturnToStart()
  31.     isAttacking = false
  32.     local distanceToStart = (klocek.Position - startedPosition).Magnitude
  33.     while distanceToStart >= 0.9 do
  34.         distanceToStart = (klocek.Position - startedPosition).Magnitude
  35.         klocek.CFrame = CFrame.new(klocek.Position,startedPosition)
  36.         klocek.Velocity = klocek.CFrame.LookVector * speedPart
  37.         wait(speed)
  38.     end
  39. end
  40.  
  41. -- this function search in workspace (model, humanoid and upperTorso) of players if not exists return nil
  42. local function SearchPlayer()
  43.     for _,v in pairs(workspace:GetChildren()) do
  44.         if v and v:IsA("Model") and v:FindFirstChild("Humanoid") then
  45.             if v.Humanoid.Health >= 1 then
  46.                
  47.                 local torso = v:FindFirstChild("UpperTorso")
  48.                 local humanoid = v.Humanoid
  49.                 local Distance = (torso.Position - klocek.Position).Magnitude
  50.                
  51.                 if Distance > max_distance then
  52.                     continue
  53.                 end
  54.                
  55.                 if torso then
  56.                     isAttacking = true
  57.                     return torso,humanoid
  58.                 else
  59.                     -- if humanoid don't have torso we return nil
  60.                     return nil
  61.                 end
  62.                
  63.             else
  64.                 -- if humanoid health have smaller health than 1 return nil
  65.                 return nil
  66.             end
  67.         end
  68.     end
  69.     -- if humanoid with model of player doesn't exists in workspace we return nil
  70.     return nil
  71. end
  72.  
  73. local function MovePart(part)
  74.     local torso,humanoid = SearchPlayer()
  75.     local distanceToStart = (klocek.Position - startedPosition).Magnitude
  76.    
  77.     if not torso and distanceToStart >= 0.9 then
  78.         return ReturnToStart()
  79.     end
  80.    
  81.     if torso then
  82.         local Distance = (torso.Position - klocek.Position).Magnitude
  83.         if Distance <= max_distance then
  84.            
  85.             klocek.CFrame = CFrame.new(klocek.Position,torso.Position)
  86.             klocek.Velocity = klocek.CFrame.LookVector * speedPart
  87.            
  88.             if Distance <= 3.5 then
  89.                 humanoid.Health -= DamagePart
  90.             end
  91.            
  92.            
  93.         elseif Distance > max_distance and distanceToStart >= 0.9 then
  94.             return ReturnToStart()
  95.         end
  96.        
  97.        
  98.     end
  99. end
  100.  
  101. while true do
  102.     MovePart(klocek)
  103.     wait(speed)
  104. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement