Advertisement
Jaden11

Gun script

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