Advertisement
HowToRoblox

TurretHandler

Jun 3rd, 2020
1,428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.48 KB | None | 0 0
  1. local sentry = script.Parent
  2.  
  3. local base = sentry:WaitForChild("Base")
  4. local turret  = sentry:WaitForChild("Turret")
  5.  
  6. local shootPart = turret:WaitForChild("ShootPart")
  7. local shootSound = shootPart:WaitForChild("ShootSound")
  8.  
  9. local isCooldown = false
  10. local timePerBullet = 0.1
  11.  
  12. local bulletDistance = 1000
  13. local maxDistance = math.huge
  14.  
  15. local maxAmmo = 500
  16. local currentAmmo = maxAmmo
  17.  
  18. local plrs = game.Players
  19.  
  20. while wait() do
  21.    
  22.     if isCooldown then return end
  23.    
  24.     local charAttacking
  25.     for i, plr in pairs(plrs:GetPlayers()) do
  26.        
  27.         local char = plr.Character
  28.        
  29.         if char and char:FindFirstChild("Humanoid") then
  30.            
  31.             if not charAttacking then charAttacking = char end
  32.            
  33.             if (shootPart.Position - char.HumanoidRootPart.Position).Magnitude < (shootPart.Position - charAttacking.HumanoidRootPart.Position).Magnitude then charAttacking = char end
  34.         end
  35.     end
  36.    
  37.     if charAttacking and (shootPart.Position - charAttacking.HumanoidRootPart.Position).Magnitude <= maxDistance and charAttacking.Humanoid.Health > 0 then
  38.        
  39.         if currentAmmo > 0 then
  40.            
  41.             isCooldown = true
  42.             currentAmmo = currentAmmo - 1
  43.            
  44.             shootSound:Play()      
  45.            
  46.             local newCFrame = CFrame.new(turret.PrimaryPart.Position, charAttacking.HumanoidRootPart.CFrame.Position)
  47.             turret:SetPrimaryPartCFrame(newCFrame)     
  48.            
  49.             local ray = Ray.new(shootPart.Position, shootPart.CFrame.LookVector * bulletDistance)  
  50.             local part, position = workspace:FindPartOnRay(ray, sentry)
  51.            
  52.             if part and part.Parent:FindFirstChild("Humanoid") then
  53.                 part.Parent.Humanoid:TakeDamage(5)
  54.             end
  55.  
  56.             local bullet = Instance.new("Part")
  57.            
  58.             bullet.CFrame = CFrame.new(shootPart.Position, shootPart.CFrame.LookVector.Unit * bulletDistance) * CFrame.new(0, 0, -(shootPart.Position - shootPart.CFrame.LookVector * bulletDistance).Magnitude / 2)
  59.             bullet.Size = Vector3.new(0.05, 0.05, (shootPart.Position - shootPart.CFrame.LookVector * bulletDistance).Magnitude)
  60.            
  61.             bullet.CanCollide = false
  62.             bullet.Anchored = true
  63.             bullet.Color = Color3.fromRGB(239, 184, 56)
  64.             bullet.Material = Enum.Material.Metal
  65.             bullet.Parent = workspace
  66.            
  67.             game.Debris:AddItem(bullet, 0.05)
  68.            
  69.             wait(timePerBullet)
  70.             isCooldown = false
  71.         end
  72.     end
  73. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement