Guest User

Untitled

a guest
Sep 14th, 2019
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. --//By: Mineloxer
  2. --//Handles back end checking + sounds
  3. --//Variables\\--
  4.  
  5. local hitmeplz = {"Arm1","Arm2","Chest","Face","Leg1","Leg2"}
  6. local player
  7. local tool = script.Parent.Parent
  8. local handle = tool:WaitForChild("Handle")
  9. local hole = tool:WaitForChild("Hole")
  10. local flash = hole.FlashGui
  11. local lastShot = tick()
  12. local equipped = false
  13. local reloading = false
  14.  
  15. --//Folders\\--
  16. local configs = tool:WaitForChild("Configs")
  17. local remotes = tool:WaitForChild("Remotes")
  18.  
  19. --//Configs\\--
  20. local ammo = configs:WaitForChild("Ammo")
  21. local reserveAmmo = configs:WaitForChild("ReserveAmmo")
  22. local magSize = configs:WaitForChild("MagSize")
  23. local damage = configs:WaitForChild("Damage")
  24. local fireRate = configs:WaitForChild("FireRate")
  25. local headMultiplier = configs:WaitForChild("HeadMultiplier")
  26. local range = configs:WaitForChild("Range")
  27. local reloadTime = configs:WaitForChild("ReloadTime")
  28.  
  29. --//Remotes\\--
  30. local canShootRemote = remotes:WaitForChild("CanShoot")
  31. local canReloadRemote = remotes:WaitForChild("CanReload")
  32. local reloadRemote = remotes:WaitForChild("Reload")
  33. local hitRemote = remotes:WaitForChild("Hit")
  34. local shootRemote = remotes:WaitForChild("Shoot")
  35.  
  36. --//Sounds\\--
  37. local emptySound = handle:WaitForChild("EmptySound")
  38. local reloadSound = handle:WaitForChild("ReloadSound")
  39. local shootSound = handle:WaitForChild("ShootSound")
  40. local headshotSound = handle:WaitForChild("HeadshotSound")
  41.  
  42. --//Functions\\--
  43. local function canReload(plr)
  44. --//Return true if gun can be reloaded, else return false
  45. if ammo.Value < magSize.Value then --//Not full ammo
  46. if reserveAmmo.Value > 0 then --//Have any ammo to reload with
  47. if not reloading then --//Not currently reloading
  48. if equipped then --//Gun is equipped in the first place
  49. return true
  50. end
  51. end
  52. end
  53. end
  54.  
  55. --//If script got here, it means that the above wasn't ful-filled so return false
  56. return false
  57. end
  58.  
  59. local function reload(plr)
  60. --//Initialize
  61. if not canReload() then return end
  62.  
  63. --//Reload
  64. reloading = true
  65. reloadSound:Play()
  66.  
  67. --//Handle ammo
  68.  
  69. ammo.Value = "REL"
  70.  
  71. --//Ensure reserveAmmo doesn't go negative
  72. if reserveAmmo.Value < 0 then
  73. ammo.Value = ammo.Value + reserveAmmo.Value --//Add the negative number as a sub
  74. reserveAmmo.Value = 0 --//Set back to 0
  75. end
  76.  
  77. wait(reloadTime.Value)
  78. local needed = magSize.Value - ammo.Value
  79. reserveAmmo.Value = reserveAmmo.Value - needed
  80. ammo.Value = magSize.Value
  81. reloading = false
  82. end
  83.  
  84. local function canShoot(plr, playSound)
  85. --//Return true if gun can be shot else return false
  86. if math.abs(lastShot - tick()) > (60/fireRate.Value) then --//Fire rate math/debounce
  87. if not reloading then --//Gun currently isn't reloading
  88. if equipped then --//Gun is equipped
  89. if ammo.Value > 0 then --//Ensure we have ammo in the mag
  90. return true
  91. else
  92. --//Play sound if needed
  93. if playSound then
  94. emptySound:Play()
  95. end
  96. end
  97. end
  98. end
  99. end
  100.  
  101. --//If script got here, it means that the above wasn't ful-filled so return false
  102. return false
  103. end
  104.  
  105. local function shoot()
  106. --//Initialize
  107. if not canShoot(player, true) then return end
  108. shootSound:Play()
  109.  
  110. flash.Enabled = true
  111. wait(.01)
  112. flash.Enabled = false
  113.  
  114. --//Handling
  115. lastShot = tick()
  116. ammo.Value = ammo.Value - 1
  117. end
  118.  
  119. local function hit(plr, part)
  120. --//Initialize || Checks
  121. if not canShoot() then return end
  122. if not part then return end
  123.  
  124. --//Get humanoid root part
  125. local character = player.Character or player.CharacterAdded:Wait()
  126. local rootPart = character:WaitForChild("HumanoidRootPart")
  127.  
  128. if rootPart then
  129. --//Check distance
  130. if (rootPart.CFrame.p - part.CFrame.p).magnitude <= range.Value then --//Check range
  131. --//Get humanoid
  132. local humanoid
  133.  
  134. --//Check if part was a hat/acessory
  135. if part.Name == "Handle" then
  136. humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
  137. else
  138. humanoid = part.Parent:FindFirstChild("Humanoid")
  139. end
  140.  
  141. for i,v in pairs(hitmeplz) do
  142. if part.Parent.Name == v then
  143. humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
  144. end
  145. end
  146.  
  147. if humanoid and humanoid.Health > 0 then --//Do nothing if humanoid is dead
  148. local newDamage = damage.Value
  149.  
  150. --//Check hit
  151. if part.Name == "Head" then
  152. newDamage = newDamage * headMultiplier.Value
  153.  
  154. --//Headshot related handling
  155. local soundCopy = headshotSound:Clone()
  156. soundCopy.Parent = part
  157. soundCopy:Play()
  158. end
  159.  
  160. --//Apply damage
  161. humanoid:TakeDamage(newDamage)
  162. end
  163. end
  164. end
  165. end
  166.  
  167. local function equip()
  168. equipped = true
  169. player = game:GetService("Players"):GetPlayerFromCharacter(tool.Parent)
  170. end
  171.  
  172. local function dequip()
  173. equipped = false
  174. end
  175.  
  176. --//Event listeners\\--
  177. tool.Equipped:Connect(equip)
  178. tool.Unequipped:Connect(dequip)
  179. hitRemote.OnServerEvent:Connect(hit)
  180. shootRemote.OnServerEvent:Connect(shoot)
  181. reloadRemote.OnServerEvent:Connect(reload)
  182. canShootRemote.OnServerInvoke = canShoot
  183. canReloadRemote.OnServerInvoke = canReload
Add Comment
Please, Sign In to add comment