Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.16 KB | None | 0 0
  1. local inCombat = false
  2. local aiming = false
  3. local threat = 0
  4. local head = script.Parent.Head
  5. local originalAngle = head.CFrame
  6. local Target
  7. local barrelorig = script.Parent.Barrel.Shooty
  8. local barrelout = script.Parent.Barrel.Shooty2
  9. local angles, radians, random, tweenserv, resume, quarantine, cfnew, heartbeat = CFrame.Angles, math.rad, math.random, game:GetService("TweenService"), coroutine.resume, coroutine.create, CFrame.new(), game:GetService("RunService").Heartbeat
  10. local impact,fired,shooting = script.Parent.Impact, script.Parent.Fired, script.Parent.Shooting
  11. local object = {}
  12.  
  13.  
  14. function object:tween(self, tweeninf, properties)
  15.     tweenserv:Create(self, tweeninf, properties):Play()
  16. end
  17.  
  18. local function ReturnMagnitude(part1,part2)
  19.     return (part1.Position - part2.Position).Magnitude
  20. end
  21.  
  22. local function Matrix(target, eye)
  23.     local forwardVector = (eye - target).Unit
  24.     local upVector = Vector3.new(0, 1, 0)
  25.     local rightVector = forwardVector:Cross(upVector)
  26.     local upVector2 = rightVector:Cross(forwardVector)
  27.  
  28.     return CFrame.fromMatrix(eye, rightVector, upVector2)
  29. end
  30.  
  31. local function Idle()
  32.     if not inCombat then
  33.         resume(quarantine(function()
  34.             while true do
  35.                 if inCombat then break end
  36.                 object:tween(head, TweenInfo.new(1, Enum.EasingStyle.Back, Enum.EasingDirection.Out), {CFrame = head.CFrame * angles(0,radians(90),0)})
  37.                 wait(1)
  38.             end
  39.         end))
  40.     end
  41. end
  42.  
  43. local function Laser(target)
  44.     local ray = Ray.new(barrelorig.WorldPosition, (barrelorig.WorldPosition - barrelorig.WorldPosition).Unit * 75)
  45.     local hit,pos = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent})
  46.     if hit and hit:FindFirstAncestorOfClass("Model") then
  47.         impact:Play()
  48.         if hit:FindFirstAncestorOfClass("Model"):FindFirstChildOfClass("Humanoid") then
  49.             hit:FindFirstAncestorOfClass("Model"):FindFirstChildOfClass("Humanoid"):TakeDamage(1)
  50.         end
  51.     end
  52. end
  53.  
  54.  
  55. local function Shoot(target)
  56.     barrelout.Parent.Beam.Enabled = true
  57.     fired:Play()
  58.     shooting:Play()
  59.     resume(quarantine(function()
  60.         while true do
  61.             Laser(target)
  62.             wait(1)
  63.         end
  64.     end))
  65. end
  66.  
  67.  
  68. local function AssessThreat(target)
  69.     Shoot(target)
  70. end
  71.  
  72.  
  73. local function Aim(target)
  74.     resume(quarantine(function()
  75.         while true do
  76.             if not aiming or target and target.Parent ~= Target then return end
  77.             head.CFrame = CFrame.new(head.Position, Matrix(head.Position, target.Position).Position)
  78.             Laser(target)
  79.             barrelout.WorldPosition = target.Position
  80.             heartbeat:Wait()
  81.         end
  82.     end))
  83. end
  84.  
  85. local function LoopWS()
  86.     local distance = 50
  87.     Target = nil
  88.     for i,v in next, workspace:GetChildren() do
  89.         if v:FindFirstChildWhichIsA("Humanoid") and v.Humanoid.Health > 0 then
  90.             local hum = v:FindFirstChildWhichIsA("Humanoid")
  91.             local root = v:FindFirstChild("HumanoidRootPart")
  92.             if root and ReturnMagnitude(barrelorig.Parent, root) < distance then
  93.                 distance = ReturnMagnitude(barrelorig.Parent, root)
  94.                 Target = v
  95.             end
  96.         end
  97.     end
  98.     return Target
  99. end
  100.  
  101. while wait(1) do
  102.     aiming = false
  103.     Target = LoopWS()
  104.     if not Target then
  105.         inCombat = false
  106.         Idle()
  107.     else
  108.         inCombat = true
  109.         aiming = true
  110.         Aim(Target.HumanoidRootPart)
  111.         AssessThreat(Target)
  112.     end
  113. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement