Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local inCombat = false
- local aiming = false
- local threat = 0
- local head = script.Parent.Head
- local originalAngle = head.CFrame
- local Target
- local barrelorig = script.Parent.Barrel.Shooty
- local barrelout = script.Parent.Barrel.Shooty2
- 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
- local impact,fired,shooting = script.Parent.Impact, script.Parent.Fired, script.Parent.Shooting
- local object = {}
- function object:tween(self, tweeninf, properties)
- tweenserv:Create(self, tweeninf, properties):Play()
- end
- local function ReturnMagnitude(part1,part2)
- return (part1.Position - part2.Position).Magnitude
- end
- local function Matrix(target, eye)
- local forwardVector = (eye - target).Unit
- local upVector = Vector3.new(0, 1, 0)
- local rightVector = forwardVector:Cross(upVector)
- local upVector2 = rightVector:Cross(forwardVector)
- return CFrame.fromMatrix(eye, rightVector, upVector2)
- end
- local function Idle()
- if not inCombat then
- resume(quarantine(function()
- while true do
- if inCombat then break end
- object:tween(head, TweenInfo.new(1, Enum.EasingStyle.Back, Enum.EasingDirection.Out), {CFrame = head.CFrame * angles(0,radians(90),0)})
- wait(1)
- end
- end))
- end
- end
- local function Laser(target)
- local ray = Ray.new(barrelorig.WorldPosition, (barrelorig.WorldPosition - barrelorig.WorldPosition).Unit * 75)
- local hit,pos = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent})
- if hit and hit:FindFirstAncestorOfClass("Model") then
- impact:Play()
- if hit:FindFirstAncestorOfClass("Model"):FindFirstChildOfClass("Humanoid") then
- hit:FindFirstAncestorOfClass("Model"):FindFirstChildOfClass("Humanoid"):TakeDamage(1)
- end
- end
- end
- local function Shoot(target)
- barrelout.Parent.Beam.Enabled = true
- fired:Play()
- shooting:Play()
- resume(quarantine(function()
- while true do
- Laser(target)
- wait(1)
- end
- end))
- end
- local function AssessThreat(target)
- Shoot(target)
- end
- local function Aim(target)
- resume(quarantine(function()
- while true do
- if not aiming or target and target.Parent ~= Target then return end
- head.CFrame = CFrame.new(head.Position, Matrix(head.Position, target.Position).Position)
- Laser(target)
- barrelout.WorldPosition = target.Position
- heartbeat:Wait()
- end
- end))
- end
- local function LoopWS()
- local distance = 50
- Target = nil
- for i,v in next, workspace:GetChildren() do
- if v:FindFirstChildWhichIsA("Humanoid") and v.Humanoid.Health > 0 then
- local hum = v:FindFirstChildWhichIsA("Humanoid")
- local root = v:FindFirstChild("HumanoidRootPart")
- if root and ReturnMagnitude(barrelorig.Parent, root) < distance then
- distance = ReturnMagnitude(barrelorig.Parent, root)
- Target = v
- end
- end
- end
- return Target
- end
- while wait(1) do
- aiming = false
- Target = LoopWS()
- if not Target then
- inCombat = false
- Idle()
- else
- inCombat = true
- aiming = true
- Aim(Target.HumanoidRootPart)
- AssessThreat(Target)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement