Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.33 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 = 35
  11. -- How many times per second the gun can fire
  12. local FireRate = 0.8 / 8
  13. -- The maximum distance the can can shoot, this value should never go above 1000
  14. local Range = 700
  15. -- In radians the minimum accuracy penalty
  16. local MinSpread = 0.01
  17. -- In radian the maximum accuracy penalty
  18. local MaxSpread = 0.02
  19. -- Number of bullets in a clip
  20. local ClipSize = 30
  21. -- DefaultValue for spare ammo
  22. local SpareAmmo = 300
  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.01
  26. -- Time it takes to reload weapon
  27. local ReloadTime = 2.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.  
  129. local function CreateBullet(bulletPos)
  130. local bullet = Instance.new('Part', Workspace)
  131. bullet.FormFactor = Enum.FormFactor.Custom
  132. bullet.Size = Vector3.new(0.1, 0.1, 0.1)
  133. bullet.BrickColor = BrickColor.new("Black")
  134. bullet.Shape = Enum.PartType.Block
  135. bullet.CanCollide = false
  136. bullet.CFrame = CFrame.new(bulletPos)
  137. bullet.Anchored = true
  138. bullet.TopSurface = Enum.SurfaceType.Smooth
  139. bullet.BottomSurface = Enum.SurfaceType.Smooth
  140. bullet.Name = 'Bullet'
  141. DebrisService:AddItem(bullet, 2.5)
  142. return bullet
  143. end
  144.  
  145. local function Reload()
  146. if not Reloading then
  147. Reloading = true
  148. -- Don't reload if you are already full or have no extra ammo
  149. if AmmoInClip ~= ClipSize and SpareAmmo > 0 then
  150. if RecoilTrack then
  151. RecoilTrack:Stop()
  152. end
  153. if WeaponGui and WeaponGui:FindFirstChild('Crosshair') then
  154. if WeaponGui.Crosshair:FindFirstChild('ReloadingLabel') then
  155. WeaponGui.Crosshair.ReloadingLabel.Visible = true
  156. end
  157. end
  158. script.Parent.Handle.Reload:Play()
  159. wait(ReloadTime)
  160. -- Only use as much ammo as you have
  161. local ammoToUse = math.min(ClipSize - AmmoInClip, SpareAmmo)
  162. AmmoInClip = AmmoInClip + ammoToUse
  163. SpareAmmo = SpareAmmo - ammoToUse
  164. UpdateAmmo(AmmoInClip)
  165. WeaponGui.Reload.Visible = false
  166. end
  167. Reloading = false
  168. end
  169. end
  170.  
  171. function OnFire()
  172. if IsShooting then return end
  173. if MyHumanoid and MyHumanoid.Health > 0 then
  174. if RecoilTrack and AmmoInClip > 0 then
  175. RecoilTrack:Play()
  176. end
  177. IsShooting = true
  178. while LeftButtonDown and AmmoInClip > 0 and not Reloading do
  179. if Spread and not DecreasedAimLastShot then
  180. Spread = math.min(MaxSpread, Spread + AimInaccuracyStepAmount)
  181. UpdateCrosshair(Spread)
  182. end
  183. DecreasedAimLastShot = not DecreasedAimLastShot
  184. if Handle:FindFirstChild('FireSound') then
  185. Handle.FireSound:Play()
  186. Handle.Flash.Enabled = true
  187. script.Parent.Flash.Light.Enabled = true
  188. script.Parent.Flash.Muzzle.Enabled = true
  189. script.Parent.Flash.Smoke.Enabled = true
  190. end
  191. if MyMouse then
  192. local targetPoint = MyMouse.Hit.p
  193. local shootDirection = (targetPoint - Handle.Position).unit
  194. -- Adjust the shoot direction randomly off by a little bit to account for recoil
  195. shootDirection = CFrame.Angles((0.5 - math.random()) * 2 * Spread,
  196. (0.5 - math.random()) * 2 * Spread,
  197. (0.5 - math.random()) * 2 * Spread) * shootDirection
  198. local hitObject, bulletPos = RayCast(Handle.Position, shootDirection, Range)
  199. local bullet
  200. -- Create a bullet here
  201. if hitObject then
  202. bullet = CreateBullet(bulletPos)
  203. end
  204. if hitObject and hitObject.Parent then
  205. local hitHumanoid = hitObject.Parent:FindFirstChild("Humanoid")
  206. if hitHumanoid then
  207. local hitPlayer = game.Players:GetPlayerFromCharacter(hitHumanoid.Parent)
  208. if MyPlayer.Neutral or hitPlayer then
  209. TagHumanoid(hitHumanoid, MyPlayer)
  210. hitHumanoid:TakeDamage(Damage)
  211. if bullet then
  212. bullet:Destroy()
  213. bullet = nil
  214. --bullet.Transparency = 1
  215. end
  216. Spawn(UpdateTargetHit)
  217. end
  218. end
  219. end
  220.  
  221. AmmoInClip = AmmoInClip - 1
  222. UpdateAmmo(AmmoInClip)
  223. end
  224. wait(FireRate)
  225. end
  226. Handle.Flash.Enabled = false
  227. script.Parent.Flash.Light.Enabled = false
  228. script.Parent.Flash.Muzzle.Enabled = false
  229. script.Parent.Flash.Smoke.Enabled = false
  230. IsShooting = false
  231. if AmmoInClip == 0 then
  232. Handle.Tick:Play()
  233. WeaponGui.Reload.Visible = true
  234. end
  235. if RecoilTrack then
  236. RecoilTrack:Stop()
  237. end
  238. end
  239. end
  240.  
  241. local TargetHits = 0
  242. function UpdateTargetHit()
  243. TargetHits = TargetHits + 1
  244. if WeaponGui and WeaponGui:FindFirstChild('Crosshair') and WeaponGui.Crosshair:FindFirstChild('TargetHitImage') then
  245. WeaponGui.Crosshair.TargetHitImage.Visible = true
  246. end
  247. wait(0.5)
  248. TargetHits = TargetHits - 1
  249. if TargetHits == 0 and WeaponGui and WeaponGui:FindFirstChild('Crosshair') and WeaponGui.Crosshair:FindFirstChild('TargetHitImage') then
  250. WeaponGui.Crosshair.TargetHitImage.Visible = false
  251. end
  252. end
  253.  
  254. function UpdateCrosshair(value, mouse)
  255. if WeaponGui then
  256. local absoluteY = 650
  257. WeaponGui.Crosshair:TweenSize(
  258. UDim2.new(0, value * absoluteY * 2 + 23, 0, value * absoluteY * 2 + 23),
  259. Enum.EasingDirection.Out,
  260. Enum.EasingStyle.Linear,
  261. 0.33)
  262. end
  263. end
  264.  
  265. function UpdateAmmo(value)
  266. if WeaponGui and WeaponGui:FindFirstChild('AmmoHud') and WeaponGui.AmmoHud:FindFirstChild('ClipAmmo') then
  267. WeaponGui.AmmoHud.ClipAmmo.Text = AmmoInClip
  268. if value > 0 and WeaponGui:FindFirstChild('Crosshair') and WeaponGui.Crosshair:FindFirstChild('ReloadingLabel') then
  269. WeaponGui.Crosshair.ReloadingLabel.Visible = false
  270. end
  271. end
  272. if WeaponGui and WeaponGui:FindFirstChild('AmmoHud') and WeaponGui.AmmoHud:FindFirstChild('TotalAmmo') then
  273. WeaponGui.AmmoHud.TotalAmmo.Text = SpareAmmo
  274. end
  275. end
  276.  
  277.  
  278. function OnMouseDown()
  279. LeftButtonDown = true
  280. OnFire()
  281. end
  282.  
  283. function OnMouseUp()
  284. LeftButtonDown = false
  285. end
  286.  
  287. function OnKeyDown(key)
  288. if string.lower(key) == 'r' then
  289. Reload()
  290. end
  291. end
  292.  
  293.  
  294. function OnEquipped(mouse)
  295. Handle.EquipSound:Play()
  296. RecoilAnim = WaitForChild(Tool, 'Recoil')
  297. FireSound = WaitForChild(Handle, 'FireSound')
  298.  
  299. MyCharacter = Tool.Parent
  300. MyPlayer = game:GetService('Players'):GetPlayerFromCharacter(MyCharacter)
  301. MyHumanoid = MyCharacter:FindFirstChild('Humanoid')
  302. MyTorso = MyCharacter:FindFirstChild('Torso')
  303. MyMouse = mouse
  304. WeaponGui = WaitForChild(Tool, 'WeaponHud'):Clone()
  305. if WeaponGui and MyPlayer then
  306. WeaponGui.Parent = MyPlayer.PlayerGui
  307. UpdateAmmo(AmmoInClip)
  308. end
  309. if RecoilAnim then
  310. RecoilTrack = MyHumanoid:LoadAnimation(RecoilAnim)
  311. end
  312.  
  313. if MyMouse then
  314. -- Disable mouse icon
  315. MyMouse.Icon = "http://www.roblox.com/asset/?id=18662154"
  316. MyMouse.Button1Down:connect(OnMouseDown)
  317. MyMouse.Button1Up:connect(OnMouseUp)
  318. MyMouse.KeyDown:connect(OnKeyDown)
  319. end
  320. end
  321.  
  322.  
  323. -- Unequip logic here
  324. function OnUnequipped()
  325. LeftButtonDown = false
  326. Reloading = false
  327. MyCharacter = nil
  328. MyHumanoid = nil
  329. MyTorso = nil
  330. MyPlayer = nil
  331. MyMouse = nil
  332. if OnFireConnection then
  333. OnFireConnection:disconnect()
  334. end
  335. if OnReloadConnection then
  336. OnReloadConnection:disconnect()
  337. end
  338. if FlashHolder then
  339. FlashHolder = nil
  340. end
  341. if WeaponGui then
  342. WeaponGui.Parent = nil
  343. WeaponGui = nil
  344. end
  345. if RecoilTrack then
  346. RecoilTrack:Stop()
  347. end
  348. end
  349.  
  350. local function SetReticleColor(color)
  351. if WeaponGui and WeaponGui:FindFirstChild('Crosshair') then
  352. for _, line in pairs(WeaponGui.Crosshair:GetChildren()) do
  353. if line:IsA('Frame') then
  354. line.BorderColor3 = color
  355. end
  356. end
  357. end
  358. end
  359.  
  360.  
  361. Tool.Equipped:connect(OnEquipped)
  362. Tool.Unequipped:connect(OnUnequipped)
  363.  
  364. while true do
  365. wait(0.033)
  366. if WeaponGui and WeaponGui:FindFirstChild('Crosshair') and MyMouse then
  367. WeaponGui.Crosshair.Position = UDim2.new(0, MyMouse.X, 0, MyMouse.Y)
  368. SetReticleColor(NeutralReticleColor)
  369.  
  370. local target = MyMouse.Target
  371. if target and target.Parent then
  372. local player = PlayersService:GetPlayerFromCharacter(target.Parent)
  373. if player then
  374. if MyPlayer.Neutral or player.TeamColor ~= MyPlayer.TeamColor then
  375. SetReticleColor(EnemyReticleColor)
  376. else
  377. SetReticleColor(FriendlyReticleColor)
  378. end
  379. end
  380. end
  381. end
  382. if Spread and not IsShooting then
  383. local currTime = time()
  384. if currTime - LastSpreadUpdate > FireRate * 2 then
  385. LastSpreadUpdate = currTime
  386. Spread = math.max(MinSpread, Spread - AimInaccuracyStepAmount)
  387. UpdateCrosshair(Spread, MyMouse)
  388. end
  389. end
  390. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement