Advertisement
Guest User

roblox gun

a guest
Jul 21st, 2019
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.83 KB | None | 0 0
  1. Made by Anthony_Dabz.
  2.  
  3. -- Local variables
  4. local tool = script.Parent
  5. local currentAmmo = tool.Configurations.ClipSize.Value
  6. local canFire = true
  7. local reloading = false
  8. local fireSound = tool.FireSound
  9.  
  10. -- Configurable variables
  11. local attackCooldown = tool.Configurations.AttackCooldown.Value
  12. local range = tool.Configurations.Range.Value
  13. local damage = tool.Configurations.Damage.Value
  14. local reloadTime = tool.Configurations.ReloadTime.Value
  15. local clipSize = tool.Configurations.ClipSize.Value
  16.  
  17. -- Setup Remote Events
  18. local function createEvent(eventName)
  19. local event = game.ReplicatedStorage:FindFirstChild(eventName)
  20. if not event then
  21. event = Instance.new("RemoteEvent", game.ReplicatedStorage)
  22. event.Name = eventName
  23. end
  24. return event
  25. end
  26. local updateEvent = createEvent("ROBLOX_PistolUpdateEvent")
  27. local equipEvent = createEvent("ROBLOX_PistolEquipEvent")
  28. local unequipEvent = createEvent("ROBLOX_PistolUnequipEvent")
  29. local fireEvent = createEvent("ROBLOX_PistolFireEvent")
  30. local reloadEvent = createEvent("ROBLOX_PistolReloadEvent")
  31.  
  32. -- Add tracer decal to server storage if it isn't already there
  33. if not game.ServerStorage:FindFirstChild("ROBLOX_PistolTracerDecal") then
  34. tool.ROBLOX_PistolTracerDecal:Clone().Parent = game.ServerStorage
  35. end
  36.  
  37. -- Bind function to update event. Used to update player's orientation if FilteringEnabled
  38. -- is true (otherwise the rotation would not replicate from the rotating player)
  39. updateEvent.OnServerEvent:connect(function(player, neckC0, rshoulderC0)
  40. local character = player.Character
  41. character.Torso.Neck.C0 = neckC0
  42. character.Torso:FindFirstChild("Right Shoulder").C0 = rshoulderC0
  43. end)
  44.  
  45. -- Bind functions to when player equips/unequips the tool. Right now just need to turn on and
  46. -- off AutoRotate
  47. equipEvent.OnServerEvent:connect(function(player)
  48. player.Character.Humanoid.AutoRotate = false
  49. end)
  50. unequipEvent.OnServerEvent:connect(function(player)
  51. player.Character.Humanoid.AutoRotate = true
  52. end)
  53.  
  54. -- Creates "bullet". No projectile motion is actually used. Pistol raytraces to target and creates
  55. -- a tracer trail to the target. Fading trail gives illusion of motion.
  56. local function createBullet(target)
  57. -- Get actual handle position. Want to offset from the center of the handle as the bullet comes
  58. -- from the barrel of the gun
  59. local handlePos = tool.Handle.CFrame + tool.Handle.CFrame:vectorToWorldSpace(Vector3.new(0,0,.3))
  60. local toTarget = handlePos:vectorToWorldSpace(Vector3.new(0,1,0)) * 200
  61. local torsoLook = (tool.Parent:FindFirstChild("Torso").CFrame.lookVector * Vector3.new(1,0,1)).unit
  62. local toTargetAngle = (toTarget * Vector3.new(1,0,1)).unit
  63. local angle = math.acos(torsoLook:Dot(toTargetAngle))
  64.  
  65. -- Checks angle from where the character is facing to the orientation of the pistol. If the angle
  66. -- is less than 90 degress then we shoot to where the mouse is pointing (helps accuracy). Otherwise
  67. -- the gun is assumed at the edge of its rotation and just shoots straight.
  68. if math.deg(angle) < 90 then
  69. toTarget = target - tool.Handle.Position
  70. if toTarget.magnitude > range then
  71. toTarget = toTarget.unit * range
  72. end
  73. toTarget = toTarget * 1.1
  74. end
  75.  
  76. -- Shoot ray and check if humanoid was hit. If so, it should take damage
  77. local ray = Ray.new(handlePos.p, toTarget)
  78. local part, position = game.Workspace:FindPartOnRay(ray, tool.Parent)
  79. if part and part.Parent and part.Parent:FindFirstChild("Humanoid") then
  80. part.Parent:FindFirstChild("Humanoid"):TakeDamage(damage)
  81. end
  82.  
  83. if position then
  84. toTarget = position - handlePos.p
  85. end
  86.  
  87. -- Create tracer trail. Trail is made of thin parts 2 studs long. Fades each segment
  88. -- starting with closest tracer to the tool.
  89. local bulletTrail = Instance.new("Model", game.Workspace)
  90. local trailTable = {}
  91. -- Fetch decal from server storage
  92. local decal = game.ServerStorage.ROBLOX_PistolTracerDecal
  93. for i = 0, toTarget.magnitude/2, 1 do
  94. local trailSegment = Instance.new("Part", bulletTrail)
  95. trailSegment.CanCollide = false
  96. trailSegment.Anchored = true
  97. trailSegment.FormFactor = Enum.FormFactor.Custom
  98. trailSegment.Size = Vector3.new(.1,.1,2)
  99. trailSegment.BrickColor = BrickColor.White()
  100. trailSegment.CFrame = CFrame.new(handlePos.p + (toTarget.unit * 2 * (i + .5)), handlePos.p)
  101. trailSegment.Transparency = 1
  102.  
  103. -- Add point light to tracer for a little illumination
  104. local light = Instance.new("PointLight", trailSegment)
  105. light.Range = 3
  106.  
  107. -- Add decal to faces of the part
  108. local function addDecal(face)
  109. local decalClone = decal:Clone()
  110. decalClone.Parent = trailSegment
  111. decalClone.Face = face
  112. end
  113.  
  114. addDecal(Enum.NormalId.Top)
  115. addDecal(Enum.NormalId.Bottom)
  116. addDecal(Enum.NormalId.Left)
  117. addDecal(Enum.NormalId.Right)
  118.  
  119. -- Add segment to all of the tracers
  120. table.insert(trailTable, trailSegment)
  121. end
  122.  
  123. -- Coroutine thread to fade each trail segment. Put in coroutine so it does not
  124. -- block the rest of the pistol's script
  125. local fadeThread = coroutine.create(function()
  126. local count = 1
  127. local ended = false
  128. -- Keep looping until end condition is met
  129. while not ended do
  130. -- Assume end condition is met. Easier to switch it off later if we need to
  131. -- keep looping
  132. ended = true
  133. -- Loop through every part in the trail
  134. for index, part in pairs(trailTable) do
  135. if index <= count then
  136. local shouldDestroy = false
  137. for _, face in pairs(part:GetChildren()) do
  138.  
  139. if face:IsA("Decal") then
  140. -- Increase decal transparencies and use this to determine if
  141. -- segment has completely faded
  142. face.Transparency = face.Transparency + .05
  143. if face.Transparency < 1 then
  144. ended = false
  145. else
  146. shouldDestroy = true
  147. end
  148. else
  149. -- Dim the point light
  150. face.Brightness = face.Brightness - .1
  151. end
  152. end
  153. -- If segment is completely faded then clean it up
  154. if shouldDestroy then
  155. table.remove(trailTable, index)
  156. part:Destroy()
  157. end
  158. end
  159. end
  160. count = count + 1
  161. wait()
  162. end
  163. bulletTrail:Destroy()
  164. end)
  165. coroutine.resume(fadeThread)
  166. end
  167.  
  168. -- Function to bind to reload event
  169. local function reload()
  170. if not reloading then
  171. tool.ReloadSound:Play()
  172. reloading = true
  173. canFire = false
  174. wait(reloadTime)
  175. currentAmmo = clipSize
  176. canFire = true
  177. reloading = false
  178. end
  179. end
  180.  
  181. reloadEvent.OnServerEvent:connect(reload)
  182.  
  183. -- Bind function to fire event
  184. fireEvent.OnServerEvent:connect(function(player, target)
  185. if tool.Parent == player.Character then
  186. -- If tool has enough shots then fires. Otherwise reloads.
  187. if currentAmmo <= 0 then
  188. return reload()
  189. end
  190. if canFire then
  191. canFire = false
  192. currentAmmo = currentAmmo - 1
  193. fireSound:Play()
  194. createBullet(target)
  195. delay(attackCooldown, function()
  196. canFire = true
  197. end)
  198. end
  199. end
  200. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement