Advertisement
Axprotss

Lazer Gun script

May 23rd, 2021
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.93 KB | None | 0 0
  1. if game:GetService("RunService"):IsClient() then error("Script must be server-side in order to work; use h/ and not hl/") end
  2. local Player,game,owner = owner,game
  3. local RealPlayer = Player
  4. do
  5.     print("FE Compatibility code by Mokiros")
  6.     local rp = RealPlayer
  7.     script.Parent = rp.Character
  8.    
  9.     --RemoteEvent for communicating
  10.     local Event = Instance.new("RemoteEvent")
  11.     Event.Name = "UserInput_Event"
  12.  
  13.     --Fake event to make stuff like Mouse.KeyDown work
  14.     local function fakeEvent()
  15.         local t = {_fakeEvent=true,Functions={},Connect=function(self,f)table.insert(self.Functions,f) end}
  16.         t.connect = t.Connect
  17.         return t
  18.     end
  19.  
  20.     --Creating fake input objects with fake variables
  21.     local m = {Target=nil,Hit=CFrame.new(),KeyUp=fakeEvent(),KeyDown=fakeEvent(),Button1Up=fakeEvent(),Button1Down=fakeEvent()}
  22.     local UIS = {InputBegan=fakeEvent(),InputEnded=fakeEvent()}
  23.     local CAS = {Actions={},BindAction=function(self,name,fun,touch,...)
  24.         CAS.Actions[name] = fun and {Name=name,Function=fun,Keys={...}} or nil
  25.     end}
  26.     --Merged 2 functions into one by checking amount of arguments
  27.     CAS.UnbindAction = CAS.BindAction
  28.  
  29.     --This function will trigger the events that have been :Connect()'ed
  30.     local function te(self,ev,...)
  31.         local t = m[ev]
  32.         if t and t._fakeEvent then
  33.             for _,f in pairs(t.Functions) do
  34.                 f(...)
  35.             end
  36.         end
  37.     end
  38.     m.TrigEvent = te
  39.     UIS.TrigEvent = te
  40.  
  41.     Event.OnServerEvent:Connect(function(plr,io)
  42.         if plr~=rp then return end
  43.         m.Target = io.Target
  44.         m.Hit = io.Hit
  45.         if not io.isMouse then
  46.             local b = io.UserInputState == Enum.UserInputState.Begin
  47.             if io.UserInputType == Enum.UserInputType.MouseButton1 then
  48.                 return m:TrigEvent(b and "Button1Down" or "Button1Up")
  49.             end
  50.             for _,t in pairs(CAS.Actions) do
  51.                 for _,k in pairs(t.Keys) do
  52.                     if k==io.KeyCode then
  53.                         t.Function(t.Name,io.UserInputState,io)
  54.                     end
  55.                 end
  56.             end
  57.             m:TrigEvent(b and "KeyDown" or "KeyUp",io.KeyCode.Name:lower())
  58.             UIS:TrigEvent(b and "InputBegan" or "InputEnded",io,false)
  59.         end
  60.     end)
  61.     Event.Parent = NLS([==[
  62.     local Player = game:GetService("Players").LocalPlayer
  63.     local Event = script:WaitForChild("UserInput_Event")
  64.  
  65.     local Mouse = Player:GetMouse()
  66.     local UIS = game:GetService("UserInputService")
  67.     local input = function(io,a)
  68.         if a then return end
  69.         --Since InputObject is a client-side instance, we create and pass table instead
  70.         Event:FireServer({KeyCode=io.KeyCode,UserInputType=io.UserInputType,UserInputState=io.UserInputState,Hit=Mouse.Hit,Target=Mouse.Target})
  71.     end
  72.     UIS.InputBegan:Connect(input)
  73.     UIS.InputEnded:Connect(input)
  74.  
  75.     local h,t
  76.     --Give the server mouse data 30 times every second, but only if the values changed
  77.     --If player is not moving their mouse, client won't fire events
  78.     while wait(1/30) do
  79.         if h~=Mouse.Hit or t~=Mouse.Target then
  80.             h,t=Mouse.Hit,Mouse.Target
  81.             Event:FireServer({isMouse=true,Target=t,Hit=h})
  82.         end
  83.     end]==],Player.Character)
  84.  
  85.     ----Sandboxed game object that allows the usage of client-side methods and services
  86.     --Real game object
  87.     local _rg = game
  88.  
  89.     --Metatable for fake service
  90.     local fsmt = {
  91.         __index = function(self,k)
  92.             local s = rawget(self,"_RealService")
  93.             if s then return s[k] end
  94.         end,
  95.         __newindex = function(self,k,v)
  96.             local s = rawget(self,"_RealService")
  97.             if s then s[k]=v end
  98.         end,
  99.         __call = function(self,...)
  100.             local s = rawget(self,"_RealService")
  101.             if s then return s(...) end
  102.         end
  103.     }
  104.     local function FakeService(t,RealService)
  105.         t._RealService = typeof(RealService)=="string" and _rg:GetService(RealService) or RealService
  106.         return setmetatable(t,fsmt)
  107.     end
  108.  
  109.     --Fake game object
  110.     local g = {
  111.         GetService = function(self,s)
  112.             return self[s]
  113.         end,
  114.         Players = FakeService({
  115.             LocalPlayer = FakeService({GetMouse=function(self)return m end},Player)
  116.         },"Players"),
  117.         UserInputService = FakeService(UIS,"UserInputService"),
  118.         ContextActionService = FakeService(CAS,"ContextActionService"),
  119.     }
  120.     rawset(g.Players,"localPlayer",g.Players.LocalPlayer)
  121.     g.service = g.GetService
  122.    
  123.     g.RunService = FakeService({
  124.         RenderStepped = _rg:GetService("RunService").Heartbeat,
  125.         BindToRenderStep = function(self,name,_,fun)
  126.             self._btrs[name] = self.Heartbeat:Connect(fun)
  127.         end,
  128.         UnbindFromRenderStep = function(self,name)
  129.             self._btrs[name]:Disconnect()
  130.         end,
  131.     },"RunService")
  132.  
  133.     setmetatable(g,{
  134.         __index=function(self,s)
  135.             return _rg:GetService(s) or typeof(_rg[s])=="function"
  136.             and function(_,...)return _rg[s](_rg,...)end or _rg[s]
  137.         end,
  138.         __newindex = fsmt.__newindex,
  139.         __call = fsmt.__call
  140.     })
  141.     --Changing owner to fake player object to support owner:GetMouse()
  142.     game,owner = g,g.Players.LocalPlayer
  143. end
  144. local p = game.Players.LocalPlayer
  145. local lol = Instance.new("Tool",p.Backpack)
  146. lol.Name = "LaserGun"
  147. lol.TextureId = "http://www.roblox.com/asset?id=130093050"
  148. local lol2 = Instance.new("Part",lol)
  149. lol2.Name = "Handle"
  150. local lol3 = Instance.new("SpecialMesh",lol2)
  151. lol3.MeshId = "http://www.roblox.com/asset?id=130099641"
  152. lol3.Scale = Vector3.new(0.65, 0.65, 0.65)
  153. lol3.TextureId = "http://www.roblox.com/asset?id=130093033"
  154. local lol4 = Instance.new("PointLight",lol2)
  155. lol4.Color = Color3.new(0, 255, 255)
  156. lol4.Range = 6
  157. local lol5 = Instance.new("Sound",lol2)
  158. lol5.Name = "Fire"
  159. lol5.SoundId = "http://www.roblox.com/asset?id=130113322"
  160. lol6 = Instance.new("Sound",lol2)
  161. lol6.Name = "Reload"
  162. lol6.SoundId = "http://www.roblox.com/asset?id=130113370"
  163. local lol7 = Instance.new("Sound",lol2)
  164. lol7.Name = "HitFade"
  165. lol7.SoundId = "http://www.roblox.com/asset?id=130113415"
  166. -----------------
  167. --| Constants |--
  168. -----------------
  169.  
  170. local SHOT_SPEED = 100
  171. local SHOT_TIME = 1
  172.  
  173. local NOZZLE_OFFSET = Vector3.new(0, 0.4, -1.1)
  174.  
  175. -----------------
  176. --| Variables |--
  177. -----------------
  178.  
  179. local PlayersService = Game:GetService('Players')
  180. local DebrisService = Game:GetService('Debris')
  181.  
  182. local Tool = lol
  183. local Handle = Tool:WaitForChild('Handle')
  184.  
  185. local FireSound = Handle:WaitForChild('Fire')
  186. local ReloadSound = Handle:WaitForChild('Reload')
  187. local HitFadeSound = Handle:WaitForChild('HitFade')
  188.  
  189. local PointLight = Handle:WaitForChild('PointLight')
  190.  
  191. local Character = nil
  192. local Humanoid = nil
  193. local Player = nil
  194.  
  195. local BaseShot = nil
  196.  
  197. -----------------
  198. --| Functions |--
  199. -----------------
  200.  
  201. -- Returns a character ancestor and its Humanoid, or nil
  202. local function FindCharacterAncestor(subject)
  203.     if subject and subject ~= Workspace then
  204.         local humanoid = subject:FindFirstChild('Humanoid')
  205.         if humanoid then
  206.             return subject, humanoid
  207.         else
  208.             return FindCharacterAncestor(subject.Parent)
  209.         end
  210.     end
  211.     return nil
  212. end
  213.  
  214. -- Removes any old creator tags and applies new ones to the specified target
  215. local function ApplyTags(target)
  216.     while target:FindFirstChild('creator') do
  217.         target.creator:Destroy()
  218.     end
  219.  
  220.     local creatorTag = Instance.new('ObjectValue')
  221.     creatorTag.Value = Player
  222.     creatorTag.Name = 'creator' --NOTE: Must be called 'creator' for website stats
  223.  
  224.     local iconTag = Instance.new('StringValue')
  225.     iconTag.Value = Tool.TextureId
  226.     iconTag.Name = 'icon'
  227.  
  228.     iconTag.Parent = creatorTag
  229.     creatorTag.Parent = target
  230.     DebrisService:AddItem(creatorTag, 4)
  231. end
  232.  
  233. -- Returns all objects under instance with Transparency
  234. local function GetTransparentsRecursive(instance, partsTable)
  235.     local partsTable = partsTable or {}
  236.     for _, child in pairs(instance:GetChildren()) do
  237.         if child:IsA('BasePart') or child:IsA('Decal') then
  238.             table.insert(partsTable, child)
  239.         end
  240.         GetTransparentsRecursive(child, partsTable)
  241.     end
  242.     return partsTable
  243. end
  244.  
  245. local function SelectionBoxify(instance)
  246.     local selectionBox = Instance.new('SelectionBox')
  247.     selectionBox.Adornee = instance
  248.     selectionBox.Color = BrickColor.new('Toothpaste')
  249.     selectionBox.Parent = instance
  250.     return selectionBox
  251. end
  252.  
  253. local function Light(instance)
  254.     local light = PointLight:Clone()
  255.     light.Range = light.Range + 2
  256.     light.Parent = instance
  257. end
  258.  
  259. local function FadeOutObjects(objectsWithTransparency, fadeIncrement)
  260.     repeat
  261.         local lastObject = nil
  262.         for _, object in pairs(objectsWithTransparency) do
  263.             object.Transparency = object.Transparency + fadeIncrement
  264.             lastObject = object
  265.         end
  266.         wait()
  267.     until lastObject.Transparency >= 1 or not lastObject
  268. end
  269.  
  270. local function Dematerialize(character, humanoid, firstPart)
  271.     humanoid.WalkSpeed = 0
  272.  
  273.     local parts = {}
  274.     for _, child in pairs(character:GetChildren()) do
  275.         if child:IsA('BasePart') then
  276.             child.Anchored = true
  277.             table.insert(parts, child)
  278.         elseif child:IsA('LocalScript') or child:IsA('Script') then
  279.             child:Destroy()
  280.         end
  281.     end
  282.  
  283.     local selectionBoxes = {}
  284.  
  285.     local firstSelectionBox = SelectionBoxify(firstPart)
  286.     Light(firstPart)
  287.     wait(0.05)
  288.  
  289.     for _, part in pairs(parts) do
  290.         if part ~= firstPart then
  291.             table.insert(selectionBoxes, SelectionBoxify(part))
  292.             Light(part)
  293.         end
  294.     end
  295.  
  296.     local objectsWithTransparency = GetTransparentsRecursive(character)
  297.     FadeOutObjects(objectsWithTransparency, 0.1)
  298.  
  299.     wait(0.5)
  300.  
  301.     humanoid.Health = 0
  302.     DebrisService:AddItem(character, 2)
  303.  
  304.     local fadeIncrement = 0.05
  305.     Delay(0.2, function()
  306.         FadeOutObjects({firstSelectionBox}, fadeIncrement)
  307.         if character then
  308.             character:Destroy()
  309.         end
  310.     end)
  311.     FadeOutObjects(selectionBoxes, fadeIncrement)
  312. end
  313.  
  314. local function OnTouched(shot, otherPart)
  315.     local character, humanoid = FindCharacterAncestor(otherPart)
  316.     if character and humanoid and character ~= Character then
  317.         ApplyTags(humanoid)
  318.         if shot then
  319.             local hitFadeSound = shot:FindFirstChild(HitFadeSound.Name)
  320.             if hitFadeSound then
  321.                 hitFadeSound.Parent = humanoid.Torso
  322.                 hitFadeSound:Play()
  323.             end
  324.             shot:Destroy()
  325.         end
  326.         Dematerialize(character, humanoid, otherPart)
  327.     end
  328. end
  329.  
  330. local function OnEquipped()
  331.     Character = Tool.Parent
  332.     Humanoid = Character:WaitForChild('Humanoid')
  333.     Player = PlayersService:GetPlayerFromCharacter(Character)
  334. end
  335.  
  336. local function OnActivated()
  337.     if Tool.Enabled and Humanoid.Health > 0 then
  338.         Tool.Enabled = false
  339.  
  340.         FireSound:Play()
  341.  
  342.         local handleCFrame = Handle.CFrame
  343.         local firingPoint = handleCFrame.p + handleCFrame:vectorToWorldSpace(NOZZLE_OFFSET)
  344.         local shotCFrame = CFrame.new(firingPoint, Humanoid.TargetPoint)
  345.  
  346.         local laserShotClone = BaseShot:Clone()
  347.         laserShotClone.CFrame = shotCFrame + (shotCFrame.lookVector * (BaseShot.Size.Z / 2))
  348.         local bodyVelocity = Instance.new('BodyVelocity')
  349.         bodyVelocity.velocity = shotCFrame.lookVector * SHOT_SPEED
  350.         bodyVelocity.Parent = laserShotClone
  351.         laserShotClone.Touched:connect(function(otherPart)
  352.             OnTouched(laserShotClone, otherPart)
  353.         end)
  354.         DebrisService:AddItem(laserShotClone, SHOT_TIME)
  355.         laserShotClone.Parent = Tool
  356.  
  357.         wait(0.6) -- FireSound length
  358.  
  359.         ReloadSound:Play()
  360.         wait(0.75) -- ReloadSound length
  361.  
  362.         Tool.Enabled = true
  363.     end
  364. end
  365.  
  366. local function OnUnequipped()
  367.    
  368. end
  369.  
  370. --------------------
  371. --| Script Logic |--
  372. --------------------
  373.  
  374. BaseShot = Instance.new('Part')
  375. BaseShot.Name = 'Effect'
  376. BaseShot.FormFactor = Enum.FormFactor.Custom
  377. BaseShot.Size = Vector3.new(0.2, 0.2, 3)
  378. BaseShot.CanCollide = false
  379. BaseShot.BrickColor = BrickColor.new('Toothpaste')
  380. SelectionBoxify(BaseShot)
  381. Light(BaseShot)
  382. HitFadeSound:Clone().Parent = BaseShot
  383.  
  384. Tool.Equipped:connect(OnEquipped)
  385. Tool.Unequipped:connect(OnUnequipped)
  386. Tool.Activated:connect(OnActivated)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement