Advertisement
HowToRoblox

TurretHandler

Sep 18th, 2021
1,641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.28 KB | None | 0 0
  1. local turret = script.Parent
  2.  
  3. turret.PrimaryPart = turret:WaitForChild("RotationPart")
  4.  
  5.  
  6. local cooldown = 1
  7. local cooldownActive = false
  8.  
  9. local maxRange = 50
  10.  
  11. local dmg = 5
  12.  
  13.  
  14.  
  15. while wait() do
  16.    
  17.    
  18.     local closestCharacter = nil
  19.    
  20.     for i, plr in pairs(game.Players:GetPlayers()) do
  21.        
  22.         if plr.Character then
  23.            
  24.             local distance = (plr.Character.HumanoidRootPart.Position - turret.PrimaryPart.Position).Magnitude
  25.            
  26.             if not closestCharacter or (closestCharacter.HumanoidRootPart.Position - turret.PrimaryPart.Position).Magnitude > distance then
  27.                
  28.                 local cf = CFrame.new(turret.PrimaryPart.Position, plr.Character.HumanoidRootPart.Position)
  29.                
  30.                 local ray = Ray.new(turret.PrimaryPart.Position, cf.LookVector * maxRange)
  31.                
  32.                 local part, position = workspace:FindPartOnRayWithIgnoreList(ray, turret.Parent:GetDescendants())
  33.                
  34.                 if part then
  35.                     local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")
  36.                    
  37.                     if humanoid == plr.Character.Humanoid and distance <= maxRange and humanoid.Health > 0 then
  38.                        
  39.                         closestCharacter = plr.Character
  40.                     end
  41.                 end
  42.             end
  43.         end
  44.     end
  45.    
  46.    
  47.     if closestCharacter then
  48.        
  49.         turret:SetPrimaryPartCFrame(turret.PrimaryPart.CFrame:Lerp(CFrame.new(turret.PrimaryPart.Position, closestCharacter.HumanoidRootPart.Position), 0.3))
  50.        
  51.         local humanoid = closestCharacter:FindFirstChild("Humanoid")
  52.            
  53.         spawn(function()
  54.            
  55.             if humanoid and not cooldownActive then
  56.                
  57.                 cooldownActive = true
  58.                
  59.                 humanoid:TakeDamage(dmg)
  60.                
  61.                
  62.                 turret.ShootPart.ShootSound:Play()
  63.                
  64.                
  65.                 local bullet = Instance.new("Part")
  66.                
  67.                 local distance = (turret.ShootPart.Position - closestCharacter.HumanoidRootPart.Position).Magnitude
  68.                
  69.                 bullet.CFrame = CFrame.new(turret.ShootPart.Position, closestCharacter.HumanoidRootPart.Position)
  70.                 bullet.CFrame = bullet.CFrame + (bullet.CFrame.LookVector * distance / 2)
  71.                
  72.                 bullet.Size = Vector3.new(0.05, 0.05, distance)
  73.                
  74.                 bullet.CanCollide = false
  75.                 bullet.Anchored = true
  76.                 bullet.Color = Color3.fromRGB(239, 184, 56)
  77.                 bullet.Parent = workspace
  78.  
  79.                 game.Debris:AddItem(bullet, 0.1)
  80.                        
  81.                    
  82.                 wait(cooldown)
  83.                 cooldownActive = false
  84.             end
  85.         end)
  86.     end
  87. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement