Advertisement
Guest User

gunClient

a guest
Jul 17th, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.87 KB | None | 0 0
  1. wait(1)
  2. --basic variables
  3. local tool = script.Parent
  4. local handle = script.Parent:WaitForChild('Handle')
  5. local model = script.Parent:WaitForChild('toolModel')
  6. local barrel = script.Parent:WaitForChild('Barrel')
  7. local camera = workspace.CurrentCamera
  8. --other essentials
  9. local rs = game:GetService('ReplicatedStorage'):WaitForChild('gunEvents')
  10. local sound = rs:WaitForChild('sound')
  11. local render = rs:WaitForChild('renderRay')
  12. local damageEvent = rs:WaitForChild('damage')
  13. local explodeEvent = rs:WaitForChild('explode')
  14. local errorHandler = rs:WaitForChild('errorHandler')
  15. local config = script.Parent:WaitForChild('config')
  16. --services
  17. local plrs = game:GetService('Players')
  18. local is = game:GetService('UserInputService')
  19. local run = game:GetService('RunService')
  20. --lol
  21. local plr = plrs.LocalPlayer
  22. local mouse = plr:GetMouse()
  23. local char = plr.Character or plr.CharacterAdded:Wait()
  24. local humanoid = char:WaitForChild('Humanoid')
  25. repeat wait() until humanoid:IsDescendantOf(workspace)
  26. --bools
  27. local sprint = false
  28. local crouching = false
  29. local reloading = false
  30. local equipped = false
  31. local canFire = true
  32. local mouseDown = false
  33. local scoping = false
  34. --config variables
  35. local spread = .5 --THE ONLY VARIABLE YOU MUST CONFIGURE IN THIS SCRIPT
  36. local ignores = {char,workspace.rayStorage}
  37. local speed = config:WaitForChild('bulletSpeed')
  38. local zoom = config:WaitForChild('zoom')
  39. local canScope = config:WaitForChild('canScope')
  40. local rayColor = config:WaitForChild('rayColor')
  41. local fireMode = config:WaitForChild('fireMode')
  42. local isLaser = config:WaitForChild('laserBullets')
  43. local ammo = config:WaitForChild('ammo')
  44. local tk = config:WaitForChild('tk')
  45. local exp = config:WaitForChild('explosion')
  46. local dmg = config:WaitForChild('damage')
  47. local fireSound = script.Parent.Handle.fire
  48. local reload = script.Parent.Handle.reload
  49. local hitmark = Instance.new('Sound')
  50. hitmark.SoundId = 'rbxassetid://160432334'
  51. hitmark.Volume = .5
  52. hitmark.Parent = plr.PlayerGui
  53. local firerate = config:WaitForChild('firerate')
  54. local reloadTime = config:WaitForChild('reloadTime')
  55. --animation stuff
  56. local holdAnim = humanoid:LoadAnimation(config:WaitForChild('holdGun'))
  57. local reloadAnim = humanoid:LoadAnimation(config:WaitForChild('reload'))
  58. local sprintAnim = humanoid:LoadAnimation(config:WaitForChild('sprint'))
  59. local crouchAnim = humanoid:LoadAnimation(config:WaitForChild('crouch'))
  60. local R15sprintAnim = humanoid:LoadAnimation(config:WaitForChild('r15sprint'))
  61. local R15holdAnim = humanoid:LoadAnimation(config:WaitForChild('r15holdGun'))
  62. local R15crouchAnim = humanoid:LoadAnimation(config:WaitForChild('r15crouch'))
  63. local R15reloadAnim = humanoid:LoadAnimation(config:WaitForChild('r15reload'))
  64. --priority declarations
  65. R15crouchAnim.Priority = Enum.AnimationPriority.Action
  66. R15holdAnim.Priority = Enum.AnimationPriority.Action
  67. R15reloadAnim.Priority = Enum.AnimationPriority.Action
  68. R15sprintAnim.Priority = Enum.AnimationPriority.Action
  69.  
  70.  
  71. --functions
  72. function flash(light)
  73.     light.Enabled=true
  74.     delay(.1,function()
  75.         light.Enabled=false
  76.     end)
  77. end
  78.  
  79. function reflect(input,normal)
  80.     return -2*input:Dot(normal)*normal+input
  81. end
  82.  
  83. function updateGUI()
  84.     if plr.PlayerGui:FindFirstChild('GunGUI') == nil then
  85.         local guiClone = script.GunGUI:Clone()
  86.         guiClone.Parent = plr.PlayerGui
  87.         guiClone.Frame.ammo.Text = ammo.Value..'/'..ammo.MaxValue
  88.         guiClone.Frame.name.Text = tool.Name
  89.         if reloading then
  90.         guiClone.Frame.reloading.TextColor3 = Color3.new(1, 0, 0.0156863)
  91.         guiClone.Frame.reloading.Text = 'RELOADING'
  92.         else
  93.         guiClone.Frame.reloading.TextColor3 = Color3.new(0, 255, 0)
  94.         guiClone.Frame.reloading.Text = 'READY'
  95.         end
  96.         if scoping then
  97.             guiClone.Scope.Visible=true
  98.             is.MouseIconEnabled=false
  99.             camera.FieldOfView=zoom.Value
  100.         elseif not scoping then
  101.             guiClone.Scope.Visible=false
  102.             is.MouseIconEnabled=true
  103.             camera.FieldOfView=70
  104.         end
  105.     else
  106.         local gui = plr.PlayerGui:FindFirstChild('GunGUI')
  107.         gui.Enabled = true
  108.         gui.Frame.ammo.Text = ammo.Value..'/'..ammo.MaxValue
  109.         gui.Frame.name.Text = tool.Name
  110.         if reloading then
  111.         gui.Frame.reloading.TextColor3 = Color3.new(1, 0, 0.0156863)
  112.         gui.Frame.reloading.Text = 'RELOADING'
  113.         else
  114.         gui.Frame.reloading.TextColor3 = Color3.new(0, 255, 0)
  115.         gui.Frame.reloading.Text = 'READY'
  116.         end
  117.         if scoping then
  118.             gui.Scope.Visible = true
  119.             is.MouseIconEnabled = false
  120.             camera.FieldOfView = zoom.Value
  121.         elseif not scoping then
  122.             gui.Scope.Visible = false
  123.             is.MouseIconEnabled = true
  124.             camera.FieldOfView=70
  125.         end
  126.     end
  127. end
  128.  
  129. function fire(mP,bPos)
  130.    
  131.     for _,v in next,barrel:GetChildren() do
  132.         if v:IsA('ParticleEmitter') then
  133.             v.Enabled = true
  134.             delay(.02,function()
  135.                 for _,v in next,barrel:GetChildren() do
  136.                     if v:IsA('ParticleEmitter') then
  137.                         v.Enabled = false
  138.                     end
  139.                 end
  140.             end)
  141.         end
  142.     end
  143.  
  144.     local mPos = mP.p + Vector3.new(math.random(-spread,spread),math.random(-spread,spread),math.random(-spread,spread))
  145.    
  146.     local b
  147.     if not isLaser.Value then
  148.         b = game:GetService('ReplicatedStorage').realBullet:Clone()
  149.     else
  150.         b = game:GetService('ReplicatedStorage').bullet:Clone()
  151.         b.ParticleEmitter.Color = ColorSequence.new(rayColor.Value)
  152.         b.ParticleEmitter:Emit(50)
  153.     end
  154.     local start = CFrame.new(bPos,mPos)*CFrame.new(0,0,-2)
  155.     b.CFrame = start
  156.     b.Parent = workspace.rayStorage
  157.     flash(barrel.flash)
  158.     sound:FireServer(fireSound)
  159.     render:FireServer(bPos,mPos,rayColor.Value,speed.Value,ignores,isLaser.Value)
  160.     ammo.Value = ammo.Value-1
  161.     updateGUI()
  162.    
  163.     local latency = 0
  164.     local lastStep = bPos
  165.     spawn(function()
  166.         while (b.CFrame.p-start.p).magnitude < 999 do
  167.             latency = run.RenderStepped:Wait()
  168.             if not isLaser.Value then
  169.                 b.CFrame = b.CFrame*CFrame.new(0,0,-latency*speed.Value)*CFrame.Angles(math.rad(-.1),0,0)
  170.             else
  171.                 b.CFrame = b.CFrame*CFrame.new(0,0,-latency*speed.Value)
  172.             end
  173.             local part,position,norm = workspace:FindPartOnRayWithIgnoreList(Ray.new(lastStep,(b.Position-lastStep).unit*(lastStep-b.Position).magnitude),ignores)
  174.             if part then
  175.                
  176.                 if part.Name:lower() == 'reflect' and isLaser.Value then
  177.                     spawn(function()
  178.                         local ref = reflect(position-bPos,norm)
  179.                         local _,refLook = workspace:FindPartOnRayWithIgnoreList(Ray.new(position,((position+ref)-position).unit*999),ignores)
  180.                         local b2 = game:GetService('ReplicatedStorage').bullet:Clone()
  181.                         local start2 = CFrame.new(position,refLook)
  182.                         b2.CFrame = start2
  183.                         b2.ParticleEmitter.Color = ColorSequence.new(rayColor.Value)
  184.                         b2.ParticleEmitter:Emit(50)
  185.                         b2.Parent = workspace.rayStorage
  186.                        
  187.                         render:FireServer(position,refLook,rayColor.Value,speed.Value,ignores,isLaser.Value)
  188.                        
  189.                         local latency2 = 0
  190.                         local lastStep2 = position
  191.                         while (b2.CFrame.p-start2.p).magnitude < 999 do
  192.                             latency2 = run.RenderStepped:Wait()
  193.                             b2.CFrame = b2.CFrame*CFrame.new(0,0,-latency2*speed.Value)
  194.                             local part2,position2,norm2 = workspace:FindPartOnRayWithIgnoreList(Ray.new(lastStep2,(b2.Position-lastStep).unit*(lastStep2-b2.Position).magnitude),ignores)
  195.                             if part2 then
  196.                                 local h2 = game:GetService('ReplicatedStorage').hit:Clone()
  197.                                 h2.Parent = workspace.rayStorage
  198.                                 h2.CFrame = CFrame.new(position2,position2+norm2)
  199.                                 game:GetService('Debris'):AddItem(h2,15)
  200.                                 for _,v in next,h2:GetChildren()do
  201.                                     if v:IsA('ParticleEmitter') or v:IsA('PointLight') then
  202.                                         v.Enabled = true
  203.                                     end
  204.                                 end
  205.                                 delay(.1,function()
  206.                                     for _,v in next,h2:GetChildren()do
  207.                                         if v:IsA('ParticleEmitter') or v:IsA('PointLight') then
  208.                                             v.Enabled = false
  209.                                         end
  210.                                     end
  211.                                 end)
  212.                                
  213.                                 if exp.Value then
  214.                                     explodeEvent:FireServer(position2)
  215.                                 end
  216.                
  217.                                 if plrs:GetPlayerFromCharacter(part2.Parent) then
  218.                                     damageEvent:FireServer(dmg.Value,part2.Parent.Humanoid)
  219.                                 elseif plrs:GetPlayerFromCharacter(part2.Parent.Parent) then
  220.                                     damageEvent:FireServer(dmg.Value,part2.Parent.Parent.Humanoid)
  221.                                 end
  222.                                 break
  223.                             end
  224.                         end
  225.                         b2:Destroy()
  226.                     end)
  227.                 end
  228.                
  229.                 if not part.Parent:FindFirstChild('Humanoid') or part.Parent.Parent:FindFirstChild('Humanoid') then
  230.                     local h = game:GetService('ReplicatedStorage').hit:Clone()
  231.                     h.Parent = workspace.rayStorage
  232.                     h.CFrame = CFrame.new(position,position+norm)
  233.                     h.Sound:Play()
  234.                     game:GetService('Debris'):AddItem(h,15)
  235.                     for _,v in next,h:GetChildren()do
  236.                         if v:IsA('ParticleEmitter') or v:IsA('PointLight') then
  237.                             v.Enabled = true
  238.                         end
  239.                     end
  240.                     delay(.1,function()
  241.                         for _,v in next,h:GetChildren()do
  242.                             if v:IsA('ParticleEmitter') or v:IsA('PointLight') then
  243.                                 v.Enabled = false
  244.                             end
  245.                         end
  246.                     end)
  247.                 else
  248.                     if not isLaser.Value then
  249.                         local h = game:GetService('ReplicatedStorage').blood:Clone()
  250.                         h.Parent = workspace.rayStorage
  251.                         h.CFrame = CFrame.new(position,position+norm)
  252.                         game:GetService('Debris'):AddItem(h,3)
  253.                         for _,v in next,h:GetChildren()do
  254.                             if v:IsA('ParticleEmitter') or v:IsA('PointLight') then
  255.                                 v.Enabled = true
  256.                             end
  257.                         end
  258.                         delay(.1,function()
  259.                             for _,v in next,h:GetChildren()do
  260.                                 if v:IsA('ParticleEmitter') or v:IsA('PointLight') then
  261.                                     v.Enabled = false
  262.                                 end
  263.                             end
  264.                         end)
  265.                     end
  266.                 end
  267.                
  268.                 if exp.Value then
  269.                     explodeEvent:FireServer(position)
  270.                 end
  271.                
  272.                 if part.Parent:FindFirstChild('Humanoid') then
  273.                     if tk.Value then
  274.                     damageEvent:FireServer(dmg.Value,part.Parent.Humanoid)
  275.                     hitmark:Play()
  276.                     elseif not tk.Value then
  277.                         if plrs:GetPlayerFromCharacter(part.Parent) then
  278.                             if plrs:GetPlayerFromCharacter(part.Parent).TeamColor ~= plr.TeamColor then
  279.                                 damageEvent:FireServer(dmg.Value,part.Parent.Humanoid)
  280.                                 hitmark:Play()
  281.                             end
  282.                         else
  283.                             damageEvent:FireServer(dmg.Value,part.Parent.Humanoid)
  284.                             hitmark:Play()
  285.                         end
  286.                     end
  287.                    
  288.                 elseif part.Parent.Parent:FindFirstChild('Humanoid') then
  289.                     if tk.Value then
  290.                     damageEvent:FireServer(dmg.Value,part.Parent.Parent.Humanoid)
  291.                     hitmark:Play()
  292.                     elseif not tk.Value then
  293.                         if plrs:GetPlayerFromCharacter(part.Parent.Parent) then
  294.                             if plrs:GetPlayerFromCharacter(part.Parent.Parent).TeamColor ~= plr.TeamColor then
  295.                                 damageEvent:FireServer(dmg.Value,part.Parent.Parent.Humanoid)
  296.                                 hitmark:Play()
  297.                             end
  298.                         else
  299.                             damageEvent:FireServer(dmg.Value,part.Parent.Parent.Humanoid)
  300.                             hitmark:Play()
  301.                         end
  302.                     end
  303.                 end
  304.                 break
  305.             end
  306.             lastStep = b.Position
  307.         end
  308.         b:Destroy()
  309.     end)
  310. end
  311.  
  312. --main framework
  313.  
  314. script.Parent.Equipped:Connect(function()
  315.     mouse.Icon = 'http://www.roblox.com/asset/?id=410597427'
  316.     equipped = true
  317.     if humanoid.RigType == Enum.HumanoidRigType.R6 then
  318.         holdAnim:Play()
  319.     else
  320.         R15holdAnim:Play()
  321.     end
  322.     updateGUI()
  323.     mouse.Button1Down:Connect(function()
  324.     if equipped then
  325.         mouseDown = true
  326.         mouse.Button1Up:Connect(function() mouseDown = false end)
  327.             if fireMode.Value == 'Semi' then
  328.                 if not reloading and ammo.Value > 0 and humanoid.Health>0 and equipped and canFire then
  329.                     if sprint then
  330.                         sprintAnim:Stop()
  331.                         sprint = false
  332.                     end
  333.                     canFire = false
  334.                     fire(mouse.Hit,barrel.Position)
  335.                     wait(firerate.Value)
  336.                     canFire = true
  337.                     end
  338.             elseif fireMode.Value == 'Auto' then
  339.                 while canFire and not reloading and ammo.Value > 0 and humanoid.Health > 0 and equipped and canFire and mouseDown do
  340.                     if sprint then
  341.                         sprintAnim:Stop()
  342.                         sprint = false
  343.                     end
  344.                     canFire=false      
  345.                     fire(mouse.Hit,barrel.CFrame.p)
  346.                     sound:FireServer(fireSound)
  347.                     wait(firerate.Value)
  348.                     canFire=true
  349.                 end
  350.             end
  351.         end
  352.     end)
  353.     mouse.Button2Down:Connect(function()
  354.         if equipped then
  355.             if canScope.Value then
  356.                 if (camera.Focus.p-camera.CoordinateFrame.p).magnitude<1 then
  357.                     scoping = true
  358.                     updateGUI()
  359.                 end
  360.             end
  361.         end
  362.     end)
  363.     mouse.Button2Up:Connect(function()
  364.         if equipped then
  365.             if canScope.Value then
  366.                 scoping = false
  367.                 updateGUI()
  368.             end
  369.         end
  370.     end)   
  371. end)
  372.    
  373. script.Parent.Unequipped:Connect(function()
  374.     mouse.Icon = ''
  375.     equipped = false
  376.     sprint = false
  377.     if humanoid.RigType == Enum.HumanoidRigType.R6 then
  378.         sprintAnim:Stop()
  379.         crouchAnim:Stop()
  380.         holdAnim:Stop()
  381.     else
  382.         R15sprintAnim:Stop()
  383.         R15crouchAnim:Stop()
  384.         R15holdAnim:Stop()
  385.     end
  386.     crouching = false
  387.     if plr.PlayerGui:FindFirstChild('GunGUI') then
  388.         plr.PlayerGui.GunGUI.Enabled=false
  389.     end
  390. end)
  391.  
  392. --KeyDown functions
  393. is.InputBegan:Connect(function(key,gpe)
  394.     if not gpe and equipped then
  395.        
  396.         if key.KeyCode == Enum.KeyCode.R then
  397.             if not sprint then
  398.                 reloading = true
  399.                 updateGUI()
  400.                 sound:FireServer(reload)
  401.                 if humanoid.RigType == Enum.HumanoidRigType.R6 then
  402.                     reloadAnim:Play()
  403.                 else
  404.                     R15reloadAnim:Play()
  405.                 end
  406.                 wait(reloadTime.Value)
  407.                 reloading = false
  408.                 ammo.Value=ammo.MaxValue
  409.                 updateGUI()
  410.             end
  411.        
  412.         elseif key.KeyCode == Enum.KeyCode.C then
  413.             if not crouching and not sitting then
  414.                 crouching = true
  415.                 if humanoid.RigType == Enum.HumanoidRigType.R6 then
  416.                     crouchAnim:Play()
  417.                 else
  418.                     R15crouchAnim:Play()           
  419.                 end
  420.             elseif crouching and not sitting then
  421.                 crouching = false
  422.                 if humanoid.RigType == Enum.HumanoidRigType.R6 then
  423.                     crouchAnim:Stop()
  424.                 else
  425.                     R15crouchAnim:Stop()
  426.                 end
  427.             end
  428.        
  429.        
  430.         elseif key.KeyCode == Enum.KeyCode.F then
  431.             if not sprint and not crouching and not sprint then
  432.                 sprint = true
  433.                 if humanoid.RigType == Enum.HumanoidRigType.R6 then
  434.                     sprintAnim:Play()
  435.                 else
  436.                     R15sprintAnim:Play()
  437.                 end
  438.         elseif sprint and not crouching then
  439.             sprint = false
  440.             if humanoid.RigType == Enum.HumanoidRigType.R6 then
  441.                 sprintAnim:Stop()
  442.             else
  443.                 R15sprintAnim:Stop()
  444.             end
  445.         end
  446.        
  447.         end
  448.     end
  449. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement