Advertisement
HowToRoblox

MobHandler

Oct 13th, 2020
5,540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.00 KB | None | 0 0
  1. local mob = script.Parent
  2.  
  3.  
  4. local mobSpeed = 14
  5. local maxHealth = 500
  6. local currentHealth = maxHealth
  7. local respawnTime = 5
  8. local damagePerHit = 30
  9. local hitCooldown = 2
  10. local hitRange = 6
  11. local senseRange = 15
  12.  
  13. local isCoolingDown = false
  14. local isAttacking = false
  15.  
  16. local spawnCFrame = mob.HumanoidRootPart.CFrame
  17.  
  18.  
  19. local humanoid = mob.Humanoid
  20.  
  21. humanoid.WalkSpeed = mobSpeed
  22.  
  23. humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
  24. humanoid.HealthDisplayType = Enum.HumanoidHealthDisplayType.AlwaysOff
  25.  
  26. humanoid.MaxHealth = 1000000
  27. humanoid.Health = 1000000
  28.  
  29.  
  30. local healthGui = mob.Head.MobHealthGui
  31. local bar = healthGui.BarBackground.Bar
  32. local healthTxt = healthGui.BarBackground.HealthText
  33. healthTxt.Text = currentHealth .. "/" .. maxHealth
  34.  
  35.  
  36. local attackAnimation = humanoid:LoadAnimation(script:WaitForChild("AttackAnimation"))
  37.  
  38.  
  39.  
  40. humanoid.Touched:Connect(function(hitPart)
  41.    
  42.     if not isAttacking then return end
  43.    
  44.    
  45.     local character = hitPart.Parent
  46.    
  47.     if character:FindFirstChild("Humanoid") then
  48.        
  49.         isAttacking = false
  50.        
  51.         character.Humanoid:TakeDamage(damagePerHit)
  52.     end
  53. end)
  54.  
  55.  
  56. local humCurrentHealth = humanoid.Health
  57.  
  58. humanoid.HealthChanged:Connect(function(newHealth)
  59.    
  60.     local healthDifference = newHealth - humCurrentHealth
  61.     humCurrentHealth = newHealth
  62.    
  63.     currentHealth = math.clamp(currentHealth + healthDifference, 0, maxHealth)
  64.    
  65.     bar.Size = UDim2.new(1/maxHealth*currentHealth, 0, 1, 0)
  66.     healthTxt.Text = currentHealth .. "/" .. maxHealth
  67.    
  68.    
  69.     if currentHealth == 0 then
  70.        
  71.         mob.Parent = game.ServerStorage
  72.        
  73.        
  74.         currentHealth = maxHealth
  75.         bar.Size = UDim2.new(1/maxHealth*currentHealth, 0, 1, 0)
  76.         healthTxt.Text = currentHealth .. "/" .. maxHealth
  77.        
  78.         humanoid.Health = humanoid.MaxHealth
  79.        
  80.        
  81.         humanoid:MoveTo(Vector3.new(0, 0, 0))
  82.         mob.HumanoidRootPart.CFrame = spawnCFrame
  83.        
  84.        
  85.         wait(respawnTime)
  86.        
  87.         mob.Parent = workspace
  88.     end
  89. end)
  90.  
  91.  
  92. game:GetService("RunService").Heartbeat:Connect(function()
  93.    
  94.    
  95.     local closestCharacter
  96.    
  97.     for i, player in pairs(game.Players:GetPlayers()) do
  98.        
  99.         local char = player.Character
  100.         if char then
  101.            
  102.             local hrp = char.HumanoidRootPart
  103.            
  104.             local distanceFromMob = (mob.HumanoidRootPart.Position - hrp.Position).Magnitude
  105.            
  106.            
  107.             if distanceFromMob <= senseRange then
  108.                
  109.                 if not closestCharacter then closestCharacter = char
  110.                    
  111.                 elseif distanceFromMob < (closestCharacter.HumanoidRootPart.Position - mob.HumanoidRootPart.Position).Magnitude then closestCharacter = char end
  112.             end    
  113.         end
  114.     end
  115.    
  116.     if closestCharacter then
  117.    
  118.         humanoid:MoveTo(closestCharacter.HumanoidRootPart.Position)
  119.        
  120.         if not isCoolingDown and (closestCharacter.HumanoidRootPart.Position - mob.HumanoidRootPart.Position).Magnitude <= hitRange then
  121.            
  122.             isCoolingDown = true
  123.            
  124.            
  125.             attackAnimation:Play()
  126.             isAttacking = true
  127.            
  128.             attackAnimation.Stopped:Wait()
  129.             isAttacking = false
  130.            
  131.            
  132.             wait(hitCooldown)
  133.             isCoolingDown = false
  134.         end
  135.     end
  136. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement