Advertisement
Guest User

turretscript

a guest
Jun 13th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.98 KB | None | 0 0
  1. sp = script.Parent
  2. r = game:GetService('RunService')
  3.  
  4. Objs = {
  5.     base = sp:WaitForChild('Base'),
  6.     turret = sp.Base:WaitForChild('Turret'),
  7. }
  8.  
  9. Set = {
  10.     ExplosionRadius = 3,
  11.     Damage = 5,
  12.     Cooldown = 0.25,
  13.     MaximumRange = 200,
  14.     MinimumRange = 2,
  15.     LockOnTime = 1.5,
  16.     TeamColor = BrickColor.new ('Cyan'),
  17.     Groups = {},
  18.     UserIds = {},
  19.     Spread = 1,
  20. }
  21.  
  22. Var = {
  23.     Target = nil,
  24.     FirstLock = 0,
  25.     RequiredLock = nil,
  26. }
  27.  
  28. Sounds = {
  29.     launch = Objs.base.PrimaryPart:WaitForChild('Launch'),
  30.     locking = Objs.base.PrimaryPart:WaitForChild('Locking'),
  31.     locked = Objs.base.PrimaryPart:WaitForChild('Locked'),
  32. }
  33.  
  34. function castray(cframeFirst, cframeSecond, ignoreObject, distanceToCast)
  35.     local newRay = Ray.new(cframeFirst.p, (cframeSecond.p - cframeFirst.p).unit * distanceToCast)
  36.     local hitObject, positionHit = workspace:FindPartOnRay(newRay, ignoreObject)
  37.     return positionHit, hitObject
  38. end
  39.  
  40. function Find(t,item,mode)
  41.     if mode == 'userid' then
  42.         for i,name in pairs (t) do
  43.             if name == item then
  44.                 return true
  45.             end
  46.         end
  47.     elseif mode == 'group' then
  48.         for i,id in pairs (t) do
  49.             if item:GetRankInGroup(id) > 0 then
  50.                 return true
  51.             end
  52.         end
  53.     end
  54.     return false
  55. end
  56.  
  57. function Fire(CFrame1,CFrame2)
  58.     Sounds.launch:Play()
  59.     local ray = Instance.new('Part',workspace)
  60.     game:GetService('Debris'):AddItem(ray,0.1)
  61.     ray.FormFactor = Enum.FormFactor.Custom
  62.     ray.TopSurface = 0
  63.     ray.BottomSurface = 0
  64.     ray.Anchored = true
  65.     ray.CanCollide = false
  66.     ray.Material = Enum.Material.Neon
  67.     ray.BrickColor = BrickColor.new('Gold')
  68.     ray.Size = Vector3.new(0.2,(CFrame1.p - CFrame2.p).magnitude,0.2)
  69.     ray.CFrame = CFrame.new((CFrame1.p + CFrame2.p)/2,CFrame1.p) * CFrame.fromEulerAnglesXYZ(math.rad(90),0,0)
  70.     local mesh = Instance.new('CylinderMesh',ray)
  71.     mesh.Scale = Vector3.new(0.25,1,0.25)
  72. end
  73.  
  74. function Explode(pos)
  75.     for i,v in pairs (game.Players:GetPlayers()) do
  76.         if v.TeamColor ~= Set.TeamColor then
  77.             if v:DistanceFromCharacter (pos) <= Set.ExplosionRadius then
  78.                 v.Character.Humanoid:TakeDamage(Set.Damage)
  79.             end
  80.         end
  81.     end
  82. end
  83.  
  84. function CheckTarget(player)
  85.     if player.TeamColor ~= Set.TeamColor or Find(Set.UserIds,player.Name,'userid') == true or Find(Set.Groups,player,'group') == true then
  86.         if player:DistanceFromCharacter(Objs.turret.PrimaryPart.Position) <= Set.MaximumRange then
  87.             if player:DistanceFromCharacter(Objs.turret.PrimaryPart.Position) >= Set.MinimumRange then
  88.                 if player.Character.Humanoid.Health > 0 then
  89.                     local pos, hit = castray(Objs.turret.Gun.CFrame, player.Character.Torso.CFrame, Objs.turret, Set.MaximumRange)
  90.                     if (pos - player.Character.Torso.Position).magnitude <= Set.ExplosionRadius then
  91.                         return true
  92.                     end
  93.                 end
  94.             end
  95.         end
  96.     end
  97.     return false
  98. end
  99.  
  100. function AquireTarget()
  101.     local targets = {}
  102.     for i,player in pairs (game.Players:GetPlayers()) do
  103.         if CheckTarget(player) == true then
  104.             table.insert(targets,player.Character)
  105.         end
  106.     end
  107.     if #targets > 0 then
  108.         Var.Target = targets[math.random(1,#targets)]
  109.     else
  110.         Var.Target = nil
  111.     end
  112. end
  113.  
  114. r.Stepped:connect(function()
  115.     if Var.Target ~= nil then
  116.         local direction = Objs.base.PrimaryPart.CFrame:pointToObjectSpace(Var.Target.Torso.Position)
  117.         local direction2 = Objs.turret.PrimaryPart.CFrame:pointToObjectSpace(Var.Target.Torso.Position)
  118.         local spiny =  math.atan2(direction.x, direction.z) + math.pi
  119.         local spinx =  math.atan2(direction2.y,direction.y)
  120.         Objs.base:SetPrimaryPartCFrame(Objs.base.PrimaryPart.CFrame * CFrame.Angles(math.rad(spiny), 0, 0))
  121.         Objs.turret:SetPrimaryPartCFrame(Objs.turret.PrimaryPart.CFrame * CFrame.Angles(math.rad(-spinx), 0, 0))
  122.     end
  123. end)
  124.  
  125. spawn(function()
  126.     while r.Stepped:wait() do
  127.         if Var.Target == nil then
  128.             for i,sound in pairs (Sounds) do
  129.                 if i ~= 'turn' and i ~= 'launch' then
  130.                     if sound.IsPlaying == true then sound:Stop() end
  131.                     wait()
  132.                 end
  133.             end
  134.             AquireTarget()
  135.         else
  136.             if Var.RequiredLock == nil then
  137.                 Var.RequiredLock = tick() + Set.LockOnTime
  138.             end
  139.             Var.FirstLock = tick()
  140.             if Sounds.locking.IsPlaying == false then Sounds.locking:Play() end
  141.             if Var.FirstLock > Var.RequiredLock then
  142.                 Sounds.locking:Stop()
  143.                 if Sounds.locked.IsPlaying == false then Sounds.locked:Play() end
  144.                 Var.RequiredLock = nil
  145.                 Var.FirstLock = 0
  146.                 while sp and wait(Set.Cooldown) and Var.Target ~= nil and CheckTarget(game.Players:GetPlayerFromCharacter(Var.Target)) == true do
  147.                     for i,gun in pairs (Objs.turret:GetChildren()) do
  148.                         if gun.Name == 'Gun' then
  149.                             local pos, hit = castray(gun.CFrame, Var.Target.Torso.CFrame, Objs.turret, Set.MaximumRange)
  150.                             gun.Muzzle.Enabled = true
  151.                             local spread = CFrame.new(math.random(-Set.Spread,Set.Spread),math.random(-Set.Spread,Set.Spread),math.random(-Set.Spread,Set.Spread))
  152.                             local tpos = CFrame.new(pos) * spread
  153.                             Fire(gun.CFrame,tpos)
  154.                             Explode(tpos.p)
  155.                             gun.Muzzle.Enabled = false
  156.                             wait()
  157.                         end
  158.                     end
  159.                 end
  160.                 Sounds.locked:Stop()
  161.                 Var.Target = nil
  162.             end
  163.         end
  164.     end
  165. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement