Ducksneedhelp1

My Gun Script

Oct 11th, 2020
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1.  
  2. -- Main Variables and defining them
  3. Tool = script.Parent
  4. Handle = Tool:WaitForChild("Handle")
  5.  
  6. Players = game:GetService("Players")
  7. Debris = game:GetService("Debris")
  8.  
  9. Speed = 100
  10. Duration = 1
  11.  
  12. NozzleOffset = Vector3.new(0, 0.4, -1.1)
  13.  
  14. Sounds = {
  15. Fire = Handle:WaitForChild("Fire"),
  16. Reload = Handle:WaitForChild("Reload"),
  17. HitFade = Handle:WaitForChild("HitFade")
  18. }
  19.  
  20. PointLight = Handle:WaitForChild("PointLight")
  21.  
  22. ServerControl = (Tool:FindFirstChild("ServerControl") or Instance.new("RemoteFunction"))
  23. ServerControl.Name = "ServerControl"
  24. ServerControl.Parent = Tool
  25.  
  26. ClientControl = (Tool:FindFirstChild("ClientControl") or Instance.new("RemoteFunction"))
  27. ClientControl.Name = "ClientControl"
  28. ClientControl.Parent = Tool
  29.  
  30. ServerControl.OnServerInvoke = (function(player, Mode, Value, arg)
  31. if player ~= Player or Humanoid.Health == 0 or not Tool.Enabled then
  32. return
  33. end
  34. if Mode == "Click" and Value then
  35. Activated(arg)
  36. end
  37. end)
  38.  
  39. function InvokeClient(Mode, Value)
  40. pcall(function()
  41. ClientControl:InvokeClient(Player, Mode, Value)
  42. end)
  43. end
  44.  
  45. function TagHumanoid(humanoid, player)
  46. local Creator_Tag = Instance.new("ObjectValue")
  47. Creator_Tag.Name = "creator"
  48. Creator_Tag.Value = player
  49. Debris:AddItem(Creator_Tag, 2)
  50. Creator_Tag.Parent = humanoid
  51. end
  52.  
  53. function UntagHumanoid(humanoid)
  54. for i, v in pairs(humanoid:GetChildren()) do
  55. if v:IsA("ObjectValue") and v.Name == "creator" then
  56. v:Destroy()
  57. end
  58. end
  59. end
  60.  
  61. function FindCharacterAncestor(Parent)
  62. if Parent and Parent ~= game:GetService("Workspace") then
  63. local humanoid = Parent:FindFirstChild("Humanoid")
  64. if humanoid then
  65. return Parent, humanoid
  66. else
  67. return FindCharacterAncestor(Parent.Parent)
  68. end
  69. end
  70. return nil
  71. end
  72.  
  73. function GetTransparentsRecursive(Parent, PartsTable)
  74. local PartsTable = (PartsTable or {})
  75. for i, v in pairs(Parent:GetChildren()) do
  76. local TransparencyExists = false
  77. pcall(function()
  78. local Transparency = v["Transparency"]
  79. if Transparency then
  80. TransparencyExists = true
  81. end
  82. end)
  83. if TransparencyExists then
  84. table.insert(PartsTable, v)
  85. end
  86. GetTransparentsRecursive(v, PartsTable)
  87. end
  88. return PartsTable
  89. end
  90.  
  91. function SelectionBoxify(Object)
  92. local SelectionBox = Instance.new("SelectionBox")
  93. SelectionBox.Adornee = Object
  94. SelectionBox.Color = BrickColor.new("Toothpaste")
  95. SelectionBox.Parent = Object
  96. return SelectionBox
  97. end
  98.  
  99. local function Light(Object)
  100. local Light = PointLight:Clone()
  101. Light.Range = (Light.Range + 2)
  102. Light.Parent = Object
  103. end
  104.  
  105. function FadeOutObjects(Objects, FadeIncrement)
  106. repeat
  107. local LastObject = nil
  108. for i, v in pairs(Objects) do
  109. v.Transparency = (v.Transparency + FadeIncrement)
  110. LastObject = v
  111. end
  112. wait()
  113. until LastObject.Transparency >= 1 or not LastObject
  114. end
  115.  
  116. function Dematerialize(character, humanoid, FirstPart)
  117. if not character or not humanoid then
  118. return
  119. end
  120.  
  121. humanoid.WalkSpeed = 0
  122.  
  123. local Parts = {}
  124.  
  125. for i, v in pairs(character:GetChildren()) do
  126. if v:IsA("BasePart") then
  127. v.Anchored = true
  128. table.insert(Parts, v)
  129. elseif v:IsA("LocalScript") or v:IsA("Script") then
  130. v:Destroy()
  131. end
  132. end
  133.  
  134. local SelectionBoxes = {}
  135.  
  136. local FirstSelectionBox = SelectionBoxify(FirstPart)
  137. Light(FirstPart)
  138. wait(0.05)
  139.  
  140. for i, v in pairs(Parts) do
  141. if v ~= FirstPart then
  142. table.insert(SelectionBoxes, SelectionBoxify(v))
  143. Light(v)
  144. end
  145. end
  146.  
  147. local ObjectsWithTransparency = GetTransparentsRecursive(character)
  148. FadeOutObjects(ObjectsWithTransparency, 0.1)
  149.  
  150. wait(0.5)
  151.  
  152. character:BreakJoints()
  153. humanoid.Health = 0
  154.  
  155. Debris:AddItem(character, 2)
  156.  
  157. local FadeIncrement = 0.05
  158. delay(0.2, function()
  159. FadeOutObjects({FirstSelectionBox}, FadeIncrement)
  160. if character and character.Parent then
  161. character:Destroy()
  162. end
  163. end)
  164. FadeOutObjects(SelectionBoxes, FadeIncrement)
  165. end
  166.  
  167. function Touched(Projectile, Hit)
  168. if not Hit or not Hit.Parent then
  169. return
  170. end
  171. local character, humanoid = FindCharacterAncestor(Hit)
  172. if character and humanoid and character ~= Character then
  173. local ForceFieldExists = false
  174. for i, v in pairs(character:GetChildren()) do
  175. if v:IsA("ForceField") then
  176. ForceFieldExists = true
  177. end
  178. end
  179. if not ForceFieldExists then
  180. if Projectile then
  181. local HitFadeSound = Projectile:FindFirstChild(Sounds.HitFade.Name)
  182. local torso = humanoid.Torso
  183. if HitFadeSound and torso then
  184. HitFadeSound.Parent = torso
  185. HitFadeSound:Play()
  186. end
  187. end
  188. Dematerialize(character, humanoid, Hit)
  189. end
  190. if Projectile and Projectile.Parent then
  191. Projectile:Destroy()
  192. end
  193. end
  194. end
  195.  
  196. function Equipped()
  197. Character = Tool.Parent
  198. Player = Players:GetPlayerFromCharacter(Character)
  199. Humanoid = Character:FindFirstChild("Humanoid")
  200. if not Player or not Humanoid or Humanoid.Health == 0 then
  201. return
  202. end
  203. end
  204.  
  205. function Activated(target)
  206. if Tool.Enabled and Humanoid.Health > 0 then
  207. Tool.Enabled = false
  208.  
  209. InvokeClient("PlaySound", Sounds.Fire)
  210.  
  211. local HandleCFrame = Handle.CFrame
  212. local FiringPoint = HandleCFrame.p + HandleCFrame:vectorToWorldSpace(NozzleOffset)
  213. local ShotCFrame = CFrame.new(FiringPoint, target)
  214.  
  215. local LaserShotClone = BaseShot:Clone()
  216. LaserShotClone.CFrame = ShotCFrame + (ShotCFrame.lookVector * (BaseShot.Size.Z / 2))
  217. local BodyVelocity = Instance.new("BodyVelocity")
  218. BodyVelocity.velocity = ShotCFrame.lookVector * Speed
  219. BodyVelocity.Parent = LaserShotClone
  220. LaserShotClone.Touched:connect(function(Hit)
  221. if not Hit or not Hit.Parent then
  222. return
  223. end
  224. Touched(LaserShotClone, Hit)
  225. end)
  226. Debris:AddItem(LaserShotClone, Duration)
  227. LaserShotClone.Parent = game:GetService("Workspace")
  228.  
  229. wait(0.6) -- FireSound length
  230.  
  231. InvokeClient("PlaySound", Sounds.Reload)
  232.  
  233. wait(0.75) -- ReloadSound length
  234.  
  235. Tool.Enabled = true
  236. end
  237. end
  238.  
  239. function Unequipped()
  240.  
  241. end
  242.  
  243. BaseShot = Instance.new("Part")
  244. BaseShot.Name = "Effect"
  245. BaseShot.BrickColor = BrickColor.new("Really black")
  246. BaseShot.Material = Enum.Material.Metal
  247. BaseShot.Shape = Enum.PartType.Block
  248. BaseShot.TopSurface = Enum.SurfaceType.Smooth
  249. BaseShot.BottomSurface = Enum.SurfaceType.Smooth
  250. BaseShot.FormFactor = Enum.FormFactor.Custom
  251. BaseShot.Size = Vector3.new(0.2, 0.2, 3)
  252. BaseShot.CanCollide = false
  253. BaseShot.Locked = true
  254. SelectionBoxify(BaseShot)
  255. Light(BaseShot)
  256. BaseShotSound = Sounds.HitFade:Clone()
  257. BaseShotSound.Parent = BaseShot
  258.  
  259. Tool.Equipped:connect(Equipped)
  260. Tool.Unequipped:connect(Unequipped)
  261. -- End of the script
Advertisement
Add Comment
Please, Sign In to add comment