Advertisement
mrmrcoder

1x1x1x1 BOSSFIGHT AI SCRIPT

Jun 3rd, 2024
2,238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.74 KB | None | 0 0
  1. --[[
  2. MADE BY MRMRCODERKID ON YOUTUBE!
  3. https://www.youtube.com/channel/UCqIm_0rGy01QNvH5dU6hbyQ
  4.  
  5. MODEL USED:
  6. https://create.roblox.com/store/asset/17638057115
  7.  
  8.  
  9. Looks for closest player
  10.     Chase down closest player
  11.         If distance is very close, Attack
  12.         Else
  13.         Continue chasing down
  14. --]]
  15.  
  16. local my_char = script.Parent
  17. local my_root = my_char.HumanoidRootPart
  18. local my_humanoid = my_char.Humanoid
  19.  
  20. local attack_cooldown = false
  21.  
  22. function GetClosestPlayer()
  23.     local selected_player = nil
  24.     local potential_players = {}
  25.     for i,p in game:GetService("Players"):GetPlayers() do
  26.         if p.Character and p.Character.PrimaryPart ~= nil then
  27.             table.insert(potential_players, p)
  28.         end
  29.     end
  30.    
  31.     for i, pPlayer in potential_players do
  32.         local smallest_dist = math.huge
  33.         local char = pPlayer.Character
  34.         local root = char.PrimaryPart
  35.         local root_rootDistance = (my_root.Position - root.Position).Magnitude
  36.        
  37.         if root_rootDistance < smallest_dist then
  38.             smallest_dist = root_rootDistance
  39.             selected_player = pPlayer
  40.         end
  41.     end
  42.    
  43.     return selected_player
  44. end
  45.  
  46. function Attack(target_humanoid)
  47.     target_humanoid:TakeDamage(10)
  48. end
  49.  
  50. while true do
  51.     task.wait()
  52.     local target = GetClosestPlayer() or nil
  53.     if target ~= nil then
  54.         local target_character = target.Character
  55.         local target_humanoid = target_character.Humanoid
  56.         local target_root = target_character.PrimaryPart
  57.        
  58.         local root_rootDistance = (my_root.Position - target_root.Position).Magnitude
  59.        
  60.         if root_rootDistance < 3 then
  61.             if attack_cooldown == false then
  62.                 Attack(target_humanoid)
  63.                 attack_cooldown = true
  64.                
  65.                 task.delay(2, function()
  66.                     attack_cooldown = false
  67.                 end)
  68.             end
  69.            
  70.         else
  71.             my_humanoid:MoveTo(target_root.Position)
  72.         end
  73.     end
  74. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement