Advertisement
Guest User

Turret

a guest
Oct 14th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.71 KB | None | 0 0
  1. local tweenservice = game:GetService("TweenService")
  2. local Gun = script.Parent.Gun
  3. local debrisservice = game:GetService("Debris")
  4. local Bullet = game:GetService("ServerStorage").Bullet
  5. local Light = script.Parent.Gun.Light
  6. local dist = 100
  7. local Targets = {}
  8.  
  9. function compare(a,b)
  10.   return a[2] < b[2]
  11. end
  12.  
  13. function Fire(Human, Distance)
  14.     local Bullet = game.ServerStorage.Bullet:Clone()
  15.     Bullet.Parent = script.Parent.Debris
  16.     Bullet.Size = Vector3.new(0.1, 0.1, 2)
  17.     Bullet.BrickColor = BrickColor.new("New Yeller")
  18.     Bullet.Anchored = true
  19.     Bullet.CanCollide = false
  20.     Bullet.Material = Enum.Material.Neon
  21.     Bullet.Transparency = 0.7
  22.     Bullet.Position = Gun.Position
  23.     Bullet.CFrame = CFrame.new(Gun.Position,Human.Head.Position)
  24.     local bullettween = tweenservice:Create(Bullet,TweenInfo.new(Distance/200,Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0,false, 0), {CFrame = Gun.CFrame:ToWorldSpace(CFrame.new(0,0,-(Distance - Bullet.Size.Z/2)))})
  25.     bullettween:Play()
  26.     bullettween:Destroy()
  27.     debrisservice:AddItem(Bullet,Distance/200)
  28. end
  29.  
  30.  
  31. function FindTarget()
  32.     for i = 1,#Targets do
  33.         table.remove(Targets,i)
  34.     end
  35.     for i,v in pairs(workspace:GetDescendants())do
  36.         if v.Name == "Head" and v.Parent:FindFirstChild("Humanoid") then
  37.             if v.Parent.Humanoid.Health > 0 then
  38.                 local ray = Ray.new(Gun.Position, (v.Position - Gun.Position).Unit *dist)
  39.                 local Part = workspace:FindPartOnRay(ray)
  40.                 if Part then
  41.                     if Part:IsDescendantOf(v.Parent) or Part.Name == "Bullet" then
  42.                         table.insert(Targets,#Targets + 1,{v.Parent,(Gun.Position - v.Position).Magnitude})
  43.                     else
  44.                     end
  45.             end
  46.             end
  47.         end
  48.     end
  49.     table.sort(Targets, compare)
  50.     if Targets[1] == nil then
  51.         return nil,nil
  52.     else
  53.         return Targets[1][1],Targets[1][2]
  54.     end
  55. end
  56. while true do
  57.     wait()
  58.     local Human,Distance = FindTarget()
  59.     if Human then
  60.         if Distance < dist then
  61.             Light.BrickColor = BrickColor.new("Bright red")
  62.             local Tween = tweenservice:Create(Gun,TweenInfo.new(0.1,Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0,false, 0), {CFrame = CFrame.new(Gun.Position,Human.Head.Position)})
  63.             Tween:Play()
  64.             Tween:Destroy()
  65.            -- Gun.CFrame = CFrame.new(Gun.Position,Human.Head.Position)
  66.             wait(0.1)
  67.             Fire(Human,Distance)
  68.             Human.Humanoid:TakeDamage(10)
  69.         else
  70.         end
  71.     else
  72.         Light.BrickColor = BrickColor.new("Lime green")
  73.         local Tween = tweenservice:Create(Gun,TweenInfo.new(0.1,Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0,false, 0), {CFrame = CFrame.new(Gun.Position) * CFrame.Angles(0,0,0)})
  74.         Tween:Play()
  75.         Tween:Destroy()
  76.     end
  77. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement