Advertisement
cheeseman50166

Untitled

Apr 30th, 2017
72
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.  
  261. if hitPlayer.Character.Humanoid.RigType.Value == 0 then
  262. newrig = false
  263. rigmeme = "Torso"
  264. else
  265. newrig = true
  266. rigmeme = "UpperTorso"
  267. end
  268. local part = Instance.new("Part", workspace)
  269. part.Size = Vector3.new(5, 5, 5)
  270. local get = Instance.new("Weld", hitPlayer.Character[rigmeme])
  271. part.Transparency = 1
  272. part.Name = "topkek"
  273. part.CanCollide = false
  274. local weld = Instance.new("Weld", part)
  275. weld.Part0 = part
  276. weld.Part1 = hitPlayer.Character[rigmeme]
  277. --local cd = Instance.new("ClickDetector", part)
  278. local plr = game.Players[get.Parent.Parent.Name]
  279. local char = plr.Character
  280. repeat
  281. wait()
  282. plr:Move(Vector3.new(math.huge*0))
  283. until char:FindFirstChild'Head' == nil
  284. end
  285. Spawn(UpdateTargetHit)
  286. end
  287. end
  288. end
  289. Kek = Instance.new ("Sound" ,game.Players.LocalPlayer.Character.AssaultRifle.Handle)
  290. Kek.Name = "FireSound"
  291. Kek.SoundId = "rbxassetid://384987591"
  292. Kek.Volume = 100
  293. Kek:Play()
  294. AmmoInClip = AmmoInClip - 1
  295. UpdateAmmo(AmmoInClip)
  296. end
  297. wait(FireRate)
  298. end
  299. IsShooting = false
  300. wait(5)
  301. local children = game.Workspace:GetChildren()
  302. for _, child in pairs(children) do
  303. for _, child in pairs(child:GetChildren()) do
  304. table.insert(children, child)
  305. end
  306.  
  307. if child:IsA("Sound") and child.Name == "FireSound" then
  308. child:Destroy()
  309. if AmmoInClip == 0 then
  310. Reload()
  311. end
  312. end
  313. end
  314. if RecoilTrack then
  315. RecoilTrack:Stop()
  316. end
  317. end
  318. end
  319. local TargetHits = 0
  320. function UpdateTargetHit()
  321. TargetHits = TargetHits + 1
  322. if WeaponGui and WeaponGui:FindFirstChild('Crosshair') and WeaponGui.Crosshair:FindFirstChild('TargetHitImage') then
  323. WeaponGui.Crosshair.TargetHitImage.Visible = true
  324. end
  325. wait(0.5)
  326. TargetHits = TargetHits - 1
  327. if TargetHits == 0 and WeaponGui and WeaponGui:FindFirstChild('Crosshair') and WeaponGui.Crosshair:FindFirstChild('TargetHitImage') then
  328. WeaponGui.Crosshair.TargetHitImage.Visible = false
  329. end
  330. end
  331.  
  332. function UpdateCrosshair(value, mouse)
  333. if WeaponGui then
  334. local absoluteY = 650
  335. WeaponGui.Crosshair:TweenSize(
  336. UDim2.new(0, value * absoluteY * 2 + 23, 0, value * absoluteY * 2 + 23),
  337. Enum.EasingDirection.Out,
  338. Enum.EasingStyle.Linear,
  339. 0.33)
  340. end
  341. end
  342.  
  343. function UpdateAmmo(value)
  344. if WeaponGui and WeaponGui:FindFirstChild('AmmoHud') and WeaponGui.AmmoHud:FindFirstChild('ClipAmmo') then
  345. WeaponGui.AmmoHud.ClipAmmo.Text = AmmoInClip
  346. if value > 0 and WeaponGui:FindFirstChild('Crosshair') and WeaponGui.Crosshair:FindFirstChild('ReloadingLabel') then
  347. WeaponGui.Crosshair.ReloadingLabel.Visible = false
  348. end
  349. end
  350. if WeaponGui and WeaponGui:FindFirstChild('AmmoHud') and WeaponGui.AmmoHud:FindFirstChild('TotalAmmo') then
  351. WeaponGui.AmmoHud.TotalAmmo.Text = SpareAmmo
  352. end
  353. end
  354.  
  355.  
  356. function OnMouseDown()
  357. LeftButtonDown = true
  358. OnFire()
  359. end
  360.  
  361. function OnMouseUp()
  362. LeftButtonDown = false
  363. end
  364.  
  365. function OnKeyDown(key)
  366. if string.lower(key) == 'r' then
  367. Reload()
  368. end
  369. end
  370.  
  371.  
  372. function OnEquipped(mouse)
  373. RecoilAnim = WaitForChild(Tool, 'Recoil')
  374.  
  375. MyCharacter = Tool.Parent
  376. MyPlayer = game:GetService('Players'):GetPlayerFromCharacter(MyCharacter)
  377. MyHumanoid = MyCharacter:FindFirstChild('Humanoid')
  378. MyTorso = MyCharacter:FindFirstChild('Torso')
  379. MyMouse = mouse
  380. WeaponGui = WaitForChild(Tool, 'WeaponHud'):Clone()
  381. if WeaponGui and MyPlayer then
  382. WeaponGui.Parent = MyPlayer.PlayerGui
  383. UpdateAmmo(AmmoInClip)
  384. end
  385. if RecoilAnim then
  386. RecoilTrack = MyHumanoid:LoadAnimation(RecoilAnim)
  387. end
  388.  
  389. if MyMouse then
  390. -- Disable mouse icon
  391. MyMouse.Icon = "http://www.roblox.com/asset/?id=18662154"
  392. MyMouse.Button1Down:connect(OnMouseDown)
  393. MyMouse.Button1Up:connect(OnMouseUp)
  394. MyMouse.KeyDown:connect(OnKeyDown)
  395. end
  396. end
  397.  
  398.  
  399. -- Unequip logic here
  400. function OnUnequipped()
  401. LeftButtonDown = false
  402. Reloading = false
  403. MyCharacter = nil
  404. MyHumanoid = nil
  405. MyTorso = nil
  406. MyPlayer = nil
  407. MyMouse = nil
  408. if OnFireConnection then
  409. OnFireConnection:disconnect()
  410. end
  411. if OnReloadConnection then
  412. OnReloadConnection:disconnect()
  413. end
  414. if FlashHolder then
  415. FlashHolder = nil
  416. end
  417. if WeaponGui then
  418. WeaponGui.Parent = nil
  419. WeaponGui = nil
  420. end
  421. if RecoilTrack then
  422. RecoilTrack:Stop()
  423. end
  424. end
  425.  
  426. local function SetReticleColor(color)
  427. if WeaponGui and WeaponGui:FindFirstChild('Crosshair') then
  428. for _, line in pairs(WeaponGui.Crosshair:GetChildren()) do
  429. if line:IsA('Frame') then
  430. line.BorderColor3 = color
  431. end
  432. end
  433. end
  434. end
  435.  
  436.  
  437. Tool.Equipped:connect(OnEquipped)
  438. Tool.Unequipped:connect(OnUnequipped)
  439.  
  440. while true do
  441. wait(0.033)
  442. if WeaponGui and WeaponGui:FindFirstChild('Crosshair') and MyMouse then
  443. WeaponGui.Crosshair.Position = UDim2.new(0, MyMouse.X, 0, MyMouse.Y)
  444. SetReticleColor(NeutralReticleColor)
  445.  
  446. local target = MyMouse.Target
  447. if target and target.Parent then
  448. local player = PlayersService:GetPlayerFromCharacter(target.Parent)
  449. if player then
  450. if MyPlayer.Neutral or player.TeamColor ~= MyPlayer.TeamColor then
  451. SetReticleColor(EnemyReticleColor)
  452. else
  453. SetReticleColor(FriendlyReticleColor)
  454. end
  455. end
  456. end
  457. end
  458. if Spread and not IsShooting then
  459. local currTime = time()
  460. if currTime - LastSpreadUpdate > FireRate * 2 then
  461. LastSpreadUpdate = currTime
  462. Spread = math.max(MinSpread, Spread - AimInaccuracyStepAmount)
  463. UpdateCrosshair(Spread, MyMouse)
  464. end
  465. end
  466. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement