HowToRoblox

MobHandler

Sep 1st, 2021 (edited)
2,982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.15 KB | None | 0 0
  1. local collectionService = game:GetService("CollectionService")
  2.  
  3.  
  4. game:GetService("RunService").Heartbeat:Connect(function()
  5.    
  6.    
  7.     for i, mob in pairs(collectionService:GetTagged("mob")) do
  8.        
  9.         spawn(function()
  10.            
  11.             if #collectionService:GetTags(mob) < 1 then return end
  12.            
  13.             local humanoid = mob:WaitForChild("Humanoid")
  14.             local hrp = mob:WaitForChild("HumanoidRootPart")
  15.            
  16.             local dmg = mob.Settings.Damage.Value
  17.             local cooldown = mob.Settings.AttackCooldown.Value
  18.             local range = mob.Settings.AttackRange.Value
  19.            
  20.             local coolingDown = mob.Settings.CoolingDown
  21.                
  22.             mob.Head.MobHealthGui.BarBackground.Bar.Size = UDim2.new(1 / humanoid.MaxHealth * humanoid.Health, 0, 1, 0)
  23.             mob.Head.MobHealthGui.BarBackground.HealthText.Text = humanoid.Health .. "/" .. humanoid.MaxHealth
  24.            
  25.            
  26.             local closestCharacter
  27.            
  28.             for i, p in pairs(game.Players:GetPlayers()) do
  29.                
  30.                 local char = p.Character
  31.                
  32.                 if char and char:FindFirstChild("HumanoidRootPart") and char.Humanoid.Health > 0 then
  33.                    
  34.                     local distance = (char.HumanoidRootPart.Position - hrp.Position).Magnitude
  35.                    
  36.                     if not closestCharacter then closestCharacter = char end
  37.                    
  38.                     local closestDistance = (closestCharacter.HumanoidRootPart.Position - hrp.Position).Magnitude
  39.                    
  40.                     if distance < closestDistance then
  41.                        
  42.                         closestCharacter = char
  43.                     end
  44.                 end
  45.             end
  46.            
  47.             if closestCharacter then
  48.                
  49.                 humanoid:MoveTo(closestCharacter.HumanoidRootPart.Position)
  50.                
  51.                
  52.                 if not coolingDown.Value then
  53.                        
  54.                     local ray = Ray.new(hrp.Position, hrp.CFrame.LookVector * range)
  55.                    
  56.                     local part = workspace:FindPartOnRay(ray, workspace.MobsFolder)
  57.                    
  58.                     if part then
  59.                    
  60.                         local plr = game.Players:GetPlayerFromCharacter(part.Parent) or game.Players:GetPlayerFromCharacter(part.Parent.Parent)
  61.    
  62.                         if plr then
  63.                            
  64.                             coolingDown.Value = true
  65.                            
  66.                             humanoid:LoadAnimation(script.AttackAnimation):Play()
  67.                            
  68.                             plr.Character.Humanoid:TakeDamage(dmg)
  69.                            
  70.                             wait(cooldown)
  71.                             coolingDown.Value = false
  72.                         end
  73.                     end
  74.                 end
  75.             end
  76.         end)
  77.     end
  78. end)
Add Comment
Please, Sign In to add comment