IamSourFish

Roblox gun script

Feb 15th, 2021
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.20 KB | None | 0 0
  1. local p = game.Players.LocalPlayer
  2. local lol = Instance.new("Tool",p.Backpack)
  3. lol.Name = "LaserGun"
  4. lol.TextureId = "http://www.roblox.com/asset?id=130093050"
  5. local lol2 = Instance.new("Part",lol)
  6. lol2.Name = "Handle"
  7. local lol3 = Instance.new("SpecialMesh",lol2)
  8. lol3.MeshId = "http://www.roblox.com/asset?id=130099641"
  9. lol3.Scale = Vector3.new(0.65, 0.65, 0.65)
  10. lol3.TextureId = "http://www.roblox.com/asset?id=130093033"
  11. local lol4 = Instance.new("PointLight",lol2)
  12. lol4.Color = Color3.new(0, 255, 255)
  13. lol4.Range = 6
  14. local lol5 = Instance.new("Sound",lol2)
  15. lol5.Name = "Fire"
  16. lol5.SoundId = "http://www.roblox.com/asset?id=130113322"
  17. lol6 = Instance.new("Sound",lol2)
  18. lol6.Name = "Reload"
  19. lol6.SoundId = "http://www.roblox.com/asset?id=130113370"
  20. local lol7 = Instance.new("Sound",lol2)
  21. lol7.Name = "HitFade"
  22. lol7.SoundId = "http://www.roblox.com/asset?id=130113415"
  23. -----------------
  24. --| Constants |--
  25. -----------------
  26.  
  27. local SHOT_SPEED = 100
  28. local SHOT_TIME = 1
  29.  
  30. local NOZZLE_OFFSET = Vector3.new(0, 0.4, -1.1)
  31.  
  32. -----------------
  33. --| Variables |--
  34. -----------------
  35.  
  36. local PlayersService = Game:GetService('Players')
  37. local DebrisService = Game:GetService('Debris')
  38.  
  39. local Tool = lol
  40. local Handle = Tool:WaitForChild('Handle')
  41.  
  42. local FireSound = Handle:WaitForChild('Fire')
  43. local ReloadSound = Handle:WaitForChild('Reload')
  44. local HitFadeSound = Handle:WaitForChild('HitFade')
  45.  
  46. local PointLight = Handle:WaitForChild('PointLight')
  47.  
  48. local Character = nil
  49. local Humanoid = nil
  50. local Player = nil
  51.  
  52. local BaseShot = nil
  53.  
  54. -----------------
  55. --| Functions |--
  56. -----------------
  57.  
  58. -- Returns a character ancestor and its Humanoid, or nil
  59. local function FindCharacterAncestor(subject)
  60. if subject and subject ~= Workspace then
  61. local humanoid = subject:FindFirstChild('Humanoid')
  62. if humanoid then
  63. return subject, humanoid
  64. else
  65. return FindCharacterAncestor(subject.Parent)
  66. end
  67. end
  68. return nil
  69. end
  70.  
  71. -- Removes any old creator tags and applies new ones to the specified target
  72. local function ApplyTags(target)
  73. while target:FindFirstChild('creator') do
  74. target.creator:Destroy()
  75. end
  76.  
  77. local creatorTag = Instance.new('ObjectValue')
  78. creatorTag.Value = Player
  79. creatorTag.Name = 'creator' --NOTE: Must be called 'creator' for website stats
  80.  
  81. local iconTag = Instance.new('StringValue')
  82. iconTag.Value = Tool.TextureId
  83. iconTag.Name = 'icon'
  84.  
  85. iconTag.Parent = creatorTag
  86. creatorTag.Parent = target
  87. DebrisService:AddItem(creatorTag, 4)
  88. end
  89.  
  90. -- Returns all objects under instance with Transparency
  91. local function GetTransparentsRecursive(instance, partsTable)
  92. local partsTable = partsTable or {}
  93. for _, child in pairs(instance:GetChildren()) do
  94. if child:IsA('BasePart') or child:IsA('Decal') then
  95. table.insert(partsTable, child)
  96. end
  97. GetTransparentsRecursive(child, partsTable)
  98. end
  99. return partsTable
  100. end
  101.  
  102. local function SelectionBoxify(instance)
  103. local selectionBox = Instance.new('SelectionBox')
  104. selectionBox.Adornee = instance
  105. selectionBox.Color = BrickColor.new('Toothpaste')
  106. selectionBox.Parent = instance
  107. return selectionBox
  108. end
  109.  
  110. local function Light(instance)
  111. local light = PointLight:Clone()
  112. light.Range = light.Range + 2
  113. light.Parent = instance
  114. end
  115.  
  116. local function FadeOutObjects(objectsWithTransparency, fadeIncrement)
  117. repeat
  118. local lastObject = nil
  119. for _, object in pairs(objectsWithTransparency) do
  120. object.Transparency = object.Transparency + fadeIncrement
  121. lastObject = object
  122. end
  123. wait()
  124. until lastObject.Transparency >= 1 or not lastObject
  125. end
  126.  
  127. local function Dematerialize(character, humanoid, firstPart)
  128. humanoid.WalkSpeed = 0
  129.  
  130. local parts = {}
  131. for _, child in pairs(character:GetChildren()) do
  132. if child:IsA('BasePart') then
  133. child.Anchored = true
  134. table.insert(parts, child)
  135. elseif child:IsA('LocalScript') or child:IsA('Script') then
  136. child:Destroy()
  137. end
  138. end
  139.  
  140. local selectionBoxes = {}
  141.  
  142. local firstSelectionBox = SelectionBoxify(firstPart)
  143. Light(firstPart)
  144. wait(0.05)
  145.  
  146. for _, part in pairs(parts) do
  147. if part ~= firstPart then
  148. table.insert(selectionBoxes, SelectionBoxify(part))
  149. Light(part)
  150. end
  151. end
  152.  
  153. local objectsWithTransparency = GetTransparentsRecursive(character)
  154. FadeOutObjects(objectsWithTransparency, 0.1)
  155.  
  156. wait(0.5)
  157.  
  158. humanoid.Health = 0
  159. DebrisService:AddItem(character, 2)
  160.  
  161. local fadeIncrement = 0.05
  162. Delay(0.2, function()
  163. FadeOutObjects({firstSelectionBox}, fadeIncrement)
  164. if character then
  165. character:Destroy()
  166. end
  167. end)
  168. FadeOutObjects(selectionBoxes, fadeIncrement)
  169. end
  170.  
  171. local function OnTouched(shot, otherPart)
  172. local character, humanoid = FindCharacterAncestor(otherPart)
  173. if character and humanoid and character ~= Character then
  174. ApplyTags(humanoid)
  175. if shot then
  176. local hitFadeSound = shot:FindFirstChild(HitFadeSound.Name)
  177. if hitFadeSound then
  178. hitFadeSound.Parent = humanoid.Torso
  179. hitFadeSound:Play()
  180. end
  181. shot:Destroy()
  182. end
  183. Dematerialize(character, humanoid, otherPart)
  184. end
  185. end
  186.  
  187. local function OnEquipped()
  188. Character = Tool.Parent
  189. Humanoid = Character:WaitForChild('Humanoid')
  190. Player = PlayersService:GetPlayerFromCharacter(Character)
  191. end
  192.  
  193. local function OnActivated()
  194. if Tool.Enabled and Humanoid.Health > 0 then
  195. Tool.Enabled = false
  196.  
  197. FireSound:Play()
  198.  
  199. local handleCFrame = Handle.CFrame
  200. local firingPoint = handleCFrame.p + handleCFrame:vectorToWorldSpace(NOZZLE_OFFSET)
  201. local shotCFrame = CFrame.new(firingPoint, Humanoid.TargetPoint)
  202.  
  203. local laserShotClone = BaseShot:Clone()
  204. laserShotClone.CFrame = shotCFrame + (shotCFrame.lookVector * (BaseShot.Size.Z / 2))
  205. local bodyVelocity = Instance.new('BodyVelocity')
  206. bodyVelocity.velocity = shotCFrame.lookVector * SHOT_SPEED
  207. bodyVelocity.Parent = laserShotClone
  208. laserShotClone.Touched:connect(function(otherPart)
  209. OnTouched(laserShotClone, otherPart)
  210. end)
  211. DebrisService:AddItem(laserShotClone, SHOT_TIME)
  212. laserShotClone.Parent = Tool
  213.  
  214. wait(0.6) -- FireSound length
  215.  
  216. ReloadSound:Play()
  217. wait(0.75) -- ReloadSound length
  218.  
  219. Tool.Enabled = true
  220. end
  221. end
  222.  
  223. local function OnUnequipped()
  224.  
  225. end
  226.  
  227. --------------------
  228. --| Script Logic |--
  229. --------------------
  230.  
  231. BaseShot = Instance.new('Part')
  232. BaseShot.Name = 'Effect'
  233. BaseShot.FormFactor = Enum.FormFactor.Custom
  234. BaseShot.Size = Vector3.new(0.2, 0.2, 3)
  235. BaseShot.CanCollide = false
  236. BaseShot.BrickColor = BrickColor.new('Toothpaste')
  237. SelectionBoxify(BaseShot)
  238. Light(BaseShot)
  239. HitFadeSound:Clone().Parent = BaseShot
  240.  
  241. Tool.Equipped:connect(OnEquipped)
  242. Tool.Unequipped:connect(OnUnequipped)
  243. Tool.Activated:connect(OnActivated)
Advertisement
Add Comment
Please, Sign In to add comment