kingsans

Untitled

Apr 2nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Credits to Paul Migo & OpTic Wisdom
  2.  
  3. game:GetObjects("rbxassetid://503182370")[1].Parent=game.Players.LocalPlayer.Backpack
  4.  
  5. game.Players.LocalPlayer.Backpack.AssaultRifle.Main.Transparency = 0
  6. game.Players.LocalPlayer.Backpack.AssaultRifle.Part1.Transparency = 0
  7. game.Players.LocalPlayer.Backpack.AssaultRifle.Part2.Transparency = 0
  8. game.Players.LocalPlayer.Backpack.AssaultRifle.Part3.Transparency = 0
  9. game.Players.LocalPlayer.Backpack.AssaultRifle.Part4.Transparency = 0
  10. game.Players.LocalPlayer.Backpack.AssaultRifle.Partkaas.Transparency = 0
  11. game.Players.LocalPlayer.Backpack.AssaultRifle.Part5.Transparency = 0
  12. game.Players.LocalPlayer.Backpack.AssaultRifle.Part6.Transparency = 0
  13. game.Players.LocalPlayer.Backpack.AssaultRifle.Part7.Transparency = 0
  14. game.Players.LocalPlayer.Backpack.AssaultRifle.Part8.Transparency = 0
  15. game.Players.LocalPlayer.Backpack.AssaultRifle.Part9.Transparency = 0
  16. game.Players.LocalPlayer.Backpack.AssaultRifle.Part10.Transparency = 0
  17. game.Players.LocalPlayer.Backpack.AssaultRifle.Part11.Transparency = 0
  18. game.Players.LocalPlayer.Backpack.AssaultRifle.Part12.Transparency = 0
  19. game.Players.LocalPlayer.Backpack.AssaultRifle.Part13.Transparency = 0
  20.  
  21. --------------------- TEMPLATE ASSAULT RIFLE WEAPON ---------------------------
  22. -- Waits for the child of the specified parent
  23. local function WaitForChild(parent, childName)
  24.     while not parent:FindFirstChild(childName) do parent.ChildAdded:wait() end
  25.     return parent[childName]
  26. end
  27.  
  28. ----- MAGIC NUMBERS ABOUT THE TOOL -----
  29. -- How much damage a bullet does
  30. local Damage = 100
  31. -- How many times per second the gun can fire
  32. local FireRate = 20 / 30
  33. -- The maximum distance the can can shoot, this value should never go above 1000
  34. local Range = 400
  35. -- In radians the minimum accuracy penalty
  36. local MinSpread = 0.01
  37. -- In radian the maximum accuracy penalty
  38. local MaxSpread = 0.1
  39. -- Number of bullets in a clip
  40. local ClipSize = 300
  41. -- DefaultValue for spare ammo
  42. local SpareAmmo = 600
  43. -- The amount the aim will increase or decrease by
  44. -- decreases this number reduces the speed that recoil takes effect
  45. local AimInaccuracyStepAmount = 0.0125
  46. -- Time it takes to reload weapon
  47. local ReloadTime = 1
  48. ----------------------------------------
  49.  
  50. -- Colors
  51. local FriendlyReticleColor = Color3.new(0, 1, 0)
  52. local EnemyReticleColor = Color3.new(1, 0, 0)
  53. local NeutralReticleColor   = Color3.new(1, 1, 1)
  54.  
  55. local Spread = MinSpread
  56. local AmmoInClip = ClipSize
  57.  
  58. local Tool = game.Players.LocalPlayer.Backpack.AssaultRifle
  59. local Handle = WaitForChild(Tool, 'Handle')
  60. local WeaponGui = nil
  61.  
  62. local LeftButtonDown
  63. local Reloading = false
  64. local IsShooting = false
  65.  
  66. -- Player specific convenience variables
  67. local MyPlayer = nil
  68. local MyCharacter = nil
  69. local MyHumanoid = nil
  70. local MyTorso = nil
  71. local MyMouse = nil
  72.  
  73. local RecoilAnim
  74. local RecoilTrack = nil
  75.  
  76. local IconURL = Tool.TextureId  -- URL to the weapon icon asset
  77.  
  78. local DebrisService = game:GetService('Debris')
  79. local PlayersService = game:GetService('Players')
  80.  
  81. local OnFireConnection = nil
  82. local OnReloadConnection = nil
  83.  
  84. local DecreasedAimLastShot = false
  85. local LastSpreadUpdate = time()
  86.  
  87. -- this is a dummy object that holds the flash made when the gun is fired
  88. local FlashHolder = nil
  89.  
  90.  
  91. local WorldToCellFunction = Workspace.Terrain.WorldToCellPreferSolid
  92. local GetCellFunction = Workspace.Terrain.GetCell
  93.  
  94. function RayIgnoreCheck(hit, pos)
  95.     if hit then
  96.         if hit.Transparency >= 1 or string.lower(hit.Name) == "water" or
  97.                 hit.Name == "Effect" or hit.Name == "Rocket" or hit.Name == "Bullet" or
  98.                 hit.Name == "Handle" or hit:IsDescendantOf(MyCharacter) then
  99.             return true
  100.         elseif hit:IsA('Terrain') and pos then
  101.             local cellPos = WorldToCellFunction(Workspace.Terrain, pos)
  102.             if cellPos then
  103.                 local cellMat = GetCellFunction(Workspace.Terrain, cellPos.x, cellPos.y, cellPos.z)
  104.                 if cellMat and cellMat == Enum.CellMaterial.Water then
  105.                     return true
  106.                 end
  107.             end
  108.         end
  109.     end
  110.     return false
  111. end
  112.  
  113. -- @preconditions: vec should be a unit vector, and 0 < rayLength <= 1000
  114. function RayCast(startPos, vec, rayLength)
  115.     local hitObject, hitPos = game.Workspace:FindPartOnRay(Ray.new(startPos + (vec * .01), vec * rayLength), Handle)
  116.     if hitObject and hitPos then
  117.         local distance = rayLength - (hitPos - startPos).magnitude
  118.         if RayIgnoreCheck(hitObject, hitPos) and distance > 0 then
  119.             -- there is a chance here for potential infinite recursion
  120.             return RayCast(hitPos, vec, distance)
  121.         end
  122.     end
  123.     return hitObject, hitPos
  124. end
  125.  
  126.  
  127.  
  128. function TagHumanoid(humanoid, player)
  129.     -- Add more tags here to customize what tags are available.
  130.     while humanoid:FindFirstChild('creator') do
  131.         humanoid:FindFirstChild('creator'):Destroy()
  132.     end
  133.     local creatorTag = Instance.new("ObjectValue")
  134.     creatorTag.Value = player
  135.     creatorTag.Name = "creator"
  136.     creatorTag.Parent = humanoid
  137.     DebrisService:AddItem(creatorTag, 1.5)
  138.  
  139.     local weaponIconTag = Instance.new("StringValue")
  140.     weaponIconTag.Value = IconURL
  141.     weaponIconTag.Name = "icon"
  142.     weaponIconTag.Parent = creatorTag
  143. end
  144.  
  145. local function CreateFlash()
  146.     if FlashHolder then
  147.         local flash = Instance.new('Fire', FlashHolder)
  148.         flash.Color = Color3.new(1, 140 / 255, 0)
  149.         flash.SecondaryColor = Color3.new(1, 0, 0)
  150.         flash.Size = 0.3
  151.         DebrisService:AddItem(flash, FireRate / 1.5)
  152.     else
  153.         FlashHolder = Instance.new("Part", Tool)
  154.         FlashHolder.Transparency = 1
  155.         FlashHolder.CanCollide= false
  156.         FlashHolder.Size = Vector3.new(1, 1, 1)
  157.         FlashHolder.Position = Tool.Handle.Position
  158.         local Weld = Instance.new("ManualWeld")
  159.         Weld.C0 = CFrame.new(0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1)
  160.         Weld.C1 = CFrame.new(0, 2.2, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0)
  161.         Weld.Part0 = FlashHolder
  162.         Weld.Part1 = Tool.Handle
  163.         Weld.Parent = FlashHolder
  164.     end
  165. end
  166.  
  167. local function CreateBullet(bulletPos)
  168.     local bullet = Instance.new('Part', Workspace)
  169.     bullettext = Instance.new("Decal" ,bullet)
  170.     bullettext1 = Instance.new("Decal" ,bullet)
  171.     bullettext1.Texture = "http://www.roblox.com/asset/?id=164688340"
  172.     bullettext2 = Instance.new("Decal" ,bullet)
  173.     bullettext2.Texture = "http://www.roblox.com/asset/?id=164688340"
  174.     bullettext1.Face = "Front"
  175.     bullettext.Texture = "http://www.roblox.com/asset/?id=164688340"
  176.     bullettext.Face = "Back"
  177.     bullettext2.Face = "Top"
  178.     bullet.FormFactor = Enum.FormFactor.Custom
  179.     bullet.Size = Vector3.new(4, 4, 0.1)
  180.     bullet.BrickColor = MyPlayer.TeamColor
  181.     bullet.Shape = Enum.PartType.Block
  182.     bullet.CanCollide = false
  183.     bullet.CFrame = CFrame.new(bulletPos)
  184.     bullet.Anchored = true
  185.     bullet.TopSurface = Enum.SurfaceType.Smooth
  186.     bullet.BottomSurface = Enum.SurfaceType.Smooth
  187.     bullet.Name = 'Bullet'
  188.     bullet.Transparency = 1
  189.     DebrisService:AddItem(bullet, 2.5)
  190.     local fire = Instance.new("Fire", bullet)
  191.     fire.Color = Color3.new(MyPlayer.TeamColor.r, MyPlayer.TeamColor.g, MyPlayer.TeamColor.b)
  192.     fire.SecondaryColor = Color3.new(MyPlayer.TeamColor.r, MyPlayer.TeamColor.g, MyPlayer.TeamColor.b)
  193.     fire.Size = 5
  194.     fire.Heat = 0
  195.     DebrisService:AddItem(fire, 0.2)
  196.     return bullet
  197. end
  198.  
  199. local function Reload()
  200.     if not Reloading then
  201.         Reloading = true
  202.         -- Don't reload if you are already full or have no extra ammo
  203.         if AmmoInClip ~= ClipSize and SpareAmmo > 0 then
  204.             if RecoilTrack then
  205.                 RecoilTrack:Stop()
  206.             end
  207.             if WeaponGui and WeaponGui:FindFirstChild('Crosshair') then
  208.                 if WeaponGui.Crosshair:FindFirstChild('ReloadingLabel') then
  209.                     WeaponGui.Crosshair.ReloadingLabel.Visible = true
  210.                 end
  211.             end
  212.             wait(ReloadTime)
  213.             -- Only use as much ammo as you have
  214.             local ammoToUse = math.min(ClipSize - AmmoInClip, SpareAmmo)
  215.             AmmoInClip = AmmoInClip + ammoToUse
  216.             SpareAmmo = SpareAmmo - ammoToUse
  217.             UpdateAmmo(AmmoInClip)
  218.         end
  219.         Reloading = false
  220.     end
  221. end
  222.  
  223. function OnFire()
  224.     if IsShooting then return end
  225.     if MyHumanoid and MyHumanoid.Health > 0 then
  226.         if RecoilTrack and AmmoInClip > 0 then
  227.             RecoilTrack:Play()
  228.         end
  229.         IsShooting = true
  230.         while LeftButtonDown and AmmoInClip > 0 and not Reloading do
  231.             if Spread and not DecreasedAimLastShot then
  232.                 Spread = math.min(MaxSpread, Spread + AimInaccuracyStepAmount)
  233.                 UpdateCrosshair(Spread)
  234.             end
  235.             CreateFlash()
  236.             if MyMouse then
  237.                 local targetPoint = MyMouse.Hit.p
  238.                 local shootDirection = (targetPoint - Handle.Position).unit
  239.                 -- Adjust the shoot direction randomly off by a little bit to account for recoil
  240.                 shootDirection = CFrame.Angles((0.5 - math.random()) * 2 * Spread,
  241.                                                                 (0.5 - math.random()) * 2 * Spread,
  242.                                                                 (0.5 - math.random()) * 2 * Spread) * shootDirection
  243.                 local hitObject, bulletPos = RayCast(Handle.Position, shootDirection, Range)
  244.                 local bullet
  245.                 -- Create a bullet here
  246.                 if hitObject then
  247.                     bullet = CreateBullet(bulletPos)
  248.                 end
  249.                 if hitObject and hitObject.Parent then
  250.                     local hitHumanoid = hitObject.Parent:FindFirstChild("Humanoid")
  251.                     if hitHumanoid then
  252.                         local hitPlayer = game.Players:GetPlayerFromCharacter(hitHumanoid.Parent)
  253.                         if MyPlayer.Neutral or (hitPlayer and hitPlayer.TeamColor ~= MyPlayer.TeamColor) then
  254.                             TagHumanoid(hitHumanoid, MyPlayer)
  255.                             hitHumanoid:TakeDamage(Damage)
  256.                             if bullet then
  257.                                 bullet:Destroy()
  258.                                 bullet = nil
  259.                                 --bullet.Transparency = 1
  260.                             end
  261.                             Spawn(UpdateTargetHit)
  262.                         end
  263.                     end
  264.                 end
  265.                 Kek = Instance.new ("Sound" ,game.Players.LocalPlayer.Character.AssaultRifle.Handle)
  266.                Kek.Name = "FireSound"
  267.                Kek.SoundId = "rbxassetid://384987591"
  268.                Kek.Volume = 100
  269.                 Kek:Play()
  270.                 AmmoInClip = AmmoInClip - 1
  271.                 UpdateAmmo(AmmoInClip)
  272.             end
  273.             wait(FireRate)
  274.         end    
  275.         IsShooting = false
  276.         wait(5)
  277.         local children = game.Workspace:GetChildren()
  278. for _, child in pairs(children) do
  279.    for _, child in pairs(child:GetChildren()) do
  280.        table.insert(children, child)
  281.    end
  282.  
  283.    if child:IsA("Sound") and child.Name == "FireSound" then
  284.            child:Destroy()
  285.         if AmmoInClip == 0 then
  286.             Reload()
  287.         end
  288. end
  289. end
  290.         if RecoilTrack then
  291.             RecoilTrack:Stop()
  292.         end
  293.     end
  294. end
  295.  
  296. local TargetHits = 0
  297. function UpdateTargetHit()
  298.     TargetHits = TargetHits + 1
  299.     if WeaponGui and WeaponGui:FindFirstChild('Crosshair') and WeaponGui.Crosshair:FindFirstChild('TargetHitImage') then
  300.         WeaponGui.Crosshair.TargetHitImage.Visible = true
  301.     end
  302.     wait(0.5)
  303.     TargetHits = TargetHits - 1
  304.     if TargetHits == 0 and WeaponGui and WeaponGui:FindFirstChild('Crosshair') and WeaponGui.Crosshair:FindFirstChild('TargetHitImage') then
  305.         WeaponGui.Crosshair.TargetHitImage.Visible = false
  306.     end
  307. end
  308.  
  309. function UpdateCrosshair(value, mouse)
  310.     if WeaponGui then
  311.         local absoluteY = 650
  312.         WeaponGui.Crosshair:TweenSize(
  313.             UDim2.new(0, value * absoluteY * 2 + 23, 0, value * absoluteY * 2 + 23),
  314.             Enum.EasingDirection.Out,
  315.             Enum.EasingStyle.Linear,
  316.             0.33)
  317.     end
  318. end
  319.  
  320. function UpdateAmmo(value)
  321.     if WeaponGui and WeaponGui:FindFirstChild('AmmoHud') and WeaponGui.AmmoHud:FindFirstChild('ClipAmmo') then
  322.         WeaponGui.AmmoHud.ClipAmmo.Text = AmmoInClip
  323.         if value > 0 and WeaponGui:FindFirstChild('Crosshair') and WeaponGui.Crosshair:FindFirstChild('ReloadingLabel') then
  324.             WeaponGui.Crosshair.ReloadingLabel.Visible = false
  325.         end
  326.     end
  327.     if WeaponGui and WeaponGui:FindFirstChild('AmmoHud') and WeaponGui.AmmoHud:FindFirstChild('TotalAmmo') then
  328.         WeaponGui.AmmoHud.TotalAmmo.Text = SpareAmmo
  329.     end
  330. end
  331.  
  332.  
  333. function OnMouseDown()
  334.     LeftButtonDown = true
  335.     OnFire()
  336. end
  337.  
  338. function OnMouseUp()
  339.     LeftButtonDown = false
  340. end
  341.  
  342. function OnKeyDown(key)
  343.     if string.lower(key) == 'r' then
  344.         Reload()
  345.     end
  346. end
  347.  
  348.  
  349. function OnEquipped(mouse)
  350.     RecoilAnim = WaitForChild(Tool, 'Recoil')
  351.  
  352.     MyCharacter = Tool.Parent
  353.     MyPlayer = game:GetService('Players'):GetPlayerFromCharacter(MyCharacter)
  354.     MyHumanoid = MyCharacter:FindFirstChild('Humanoid')
  355.     MyTorso = MyCharacter:FindFirstChild('Torso')
  356.     MyMouse = mouse
  357.     WeaponGui = WaitForChild(Tool, 'WeaponHud'):Clone()
  358.     if WeaponGui and MyPlayer then
  359.         WeaponGui.Parent = MyPlayer.PlayerGui
  360.         UpdateAmmo(AmmoInClip)
  361.     end
  362.     if RecoilAnim then
  363.         RecoilTrack = MyHumanoid:LoadAnimation(RecoilAnim)
  364.     end
  365.  
  366.     if MyMouse then
  367.         -- Disable mouse icon
  368.         MyMouse.Icon = "http://www.roblox.com/asset/?id=18662154"
  369.         MyMouse.Button1Down:connect(OnMouseDown)
  370.         MyMouse.Button1Up:connect(OnMouseUp)
  371.         MyMouse.KeyDown:connect(OnKeyDown)
  372.     end
  373. end
  374.  
  375.  
  376. -- Unequip logic here
  377. function OnUnequipped()
  378.     LeftButtonDown = false
  379.     Reloading = false
  380.     MyCharacter = nil
  381.     MyHumanoid = nil
  382.     MyTorso = nil
  383.     MyPlayer = nil
  384.     MyMouse = nil
  385.     if OnFireConnection then
  386.         OnFireConnection:disconnect()
  387.     end
  388.     if OnReloadConnection then
  389.         OnReloadConnection:disconnect()
  390.     end
  391.     if FlashHolder then
  392.         FlashHolder = nil
  393.     end
  394.     if WeaponGui then
  395.         WeaponGui.Parent = nil
  396.         WeaponGui = nil
  397.     end
  398.     if RecoilTrack then
  399.         RecoilTrack:Stop()
  400.     end
  401. end
  402.  
  403. local function SetReticleColor(color)
  404.     if WeaponGui and WeaponGui:FindFirstChild('Crosshair') then
  405.         for _, line in pairs(WeaponGui.Crosshair:GetChildren()) do
  406.             if line:IsA('Frame') then
  407.                 line.BorderColor3 = color
  408.             end
  409.         end
  410.     end
  411. end
  412.  
  413.  
  414. Tool.Equipped:connect(OnEquipped)
  415. Tool.Unequipped:connect(OnUnequipped)
  416.  
  417. while true do
  418.     wait(0.033)
  419.     if WeaponGui and WeaponGui:FindFirstChild('Crosshair') and MyMouse then
  420.         WeaponGui.Crosshair.Position = UDim2.new(0, MyMouse.X, 0, MyMouse.Y)
  421.         SetReticleColor(NeutralReticleColor)
  422.  
  423.         local target = MyMouse.Target
  424.         if target and target.Parent then
  425.             local player = PlayersService:GetPlayerFromCharacter(target.Parent)
  426.             if player then
  427.                 if MyPlayer.Neutral or player.TeamColor ~= MyPlayer.TeamColor then
  428.                     SetReticleColor(EnemyReticleColor)
  429.                 else
  430.                     SetReticleColor(FriendlyReticleColor)
  431.                 end
  432.             end
  433.         end
  434.     end
  435.     if Spread and not IsShooting then
  436.         local currTime = time()
  437.         if currTime - LastSpreadUpdate > FireRate * 2 then
  438.             LastSpreadUpdate = currTime
  439.             Spread = math.max(MinSpread, Spread - AimInaccuracyStepAmount)
  440.             UpdateCrosshair(Spread, MyMouse)
  441.         end
  442.     end
  443. end
Add Comment
Please, Sign In to add comment