Advertisement
TheGamerKing

Assault Rifle Script

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