Advertisement
HowToRoblox

GunClient

Jul 12th, 2022
2,905
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.86 KB | None | 0 0
  1. local character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
  2.  
  3. local animations = {
  4.     equip = character:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("EquipAnimation")),
  5.     hold = character:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("HoldAnimation")),
  6.     reload = character:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("ReloadAnimation")),
  7.     shoot = character:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("ShootAnimation")),
  8. }
  9.  
  10. local remoteEvent = game.ReplicatedStorage:WaitForChild("GunRE")
  11.  
  12. local gun = script.Parent
  13. local gunSettings = require(gun.GunSettings)
  14.  
  15. local uis = game:GetService("UserInputService")
  16.  
  17. local mouse = game.Players.LocalPlayer:GetMouse()
  18. local camera = workspace.CurrentCamera
  19.  
  20. local mouseDown = false
  21.  
  22. local cameraOffset = 0
  23.  
  24.  
  25. --Equipping
  26. gun.Equipped:Connect(function()
  27.     remoteEvent:FireServer("ConnectM6D", gun)
  28.  
  29.     character.UpperTorso.GunMotor6D.Part0 = character.UpperTorso
  30.     character.UpperTorso.GunMotor6D.Part1 = gun.BodyAttach
  31.  
  32.     game.Players.LocalPlayer.PlayerGui.GunGui.AmmoFrame.GunLabel.Text = gun.Name
  33.     game.Players.LocalPlayer.PlayerGui.GunGui.AmmoFrame.AmmoLabel.Text = gun.CurrentAmmo.Value
  34.     game.Players.LocalPlayer.PlayerGui.GunGui.Enabled = true
  35.     uis.MouseIconEnabled = false
  36.  
  37.     animations["equip"]:Play()
  38.     animations["equip"].Stopped:Wait()
  39.     animations["hold"]:Play()
  40. end)
  41. --Unequipping
  42. gun.Unequipped:Connect(function()
  43.     remoteEvent:FireServer("DisconnectM6D", gun)
  44.  
  45.     game.Players.LocalPlayer.PlayerGui.GunGui.Enabled = false
  46.     uis.MouseIconEnabled = true
  47.  
  48.     for i, animation in pairs(animations) do
  49.         animation:Stop()
  50.     end
  51. end)
  52.  
  53. --Update ammo GUI when bullets change
  54. gun.CurrentAmmo:GetPropertyChangedSignal("Value"):Connect(function()
  55.     if gun.Parent == character then
  56.         game.Players.LocalPlayer.PlayerGui.GunGui.AmmoFrame.AmmoLabel.Text = gun.CurrentAmmo.Value
  57.     end
  58. end)
  59.  
  60. --Clicking to shoot
  61. mouse.Button1Down:Connect(function()
  62.     mouseDown = true
  63.  
  64.     if gunSettings["automatic"] == true then
  65.         repeat
  66.             remoteEvent:FireServer("Shoot", gun, camera.CFrame, mouse.Hit)
  67.             wait()
  68.         until mouseDown == false
  69.  
  70.     else
  71.         remoteEvent:FireServer("Shoot", gun, camera.CFrame, mouse.Hit)
  72.     end
  73. end)
  74. --Letting go of click to stop shooting
  75. mouse.Button1Up:Connect(function()
  76.     mouseDown = false
  77. end)
  78.  
  79. --Reload when player presses R
  80. uis.InputBegan:Connect(function(inp, p)
  81.     if not p and inp.KeyCode == Enum.KeyCode.R then
  82.         remoteEvent:FireServer("Reload", gun)
  83.     end
  84. end)
  85.  
  86. --Apply recoil to camera and crosshair
  87. game:GetService("RunService").Stepped:Connect(function()
  88.     if gun.Parent == character then
  89.         camera.CFrame = camera.CFrame:Lerp(camera.CFrame * CFrame.Angles(cameraOffset / 30, 0, 0), 0.2)
  90.         cameraOffset = math.clamp(cameraOffset - 0.1, 0, math.huge)
  91.  
  92.         local crosshair = game.Players.LocalPlayer.PlayerGui.GunGui.CrosshairFrame
  93.         crosshair.Position = UDim2.new(0, mouse.X, 0, mouse.Y)
  94.         crosshair.Top.Position = UDim2.new(crosshair.Top.Position.X.Scale, 0, crosshair.Top.Position.Y.Scale, -math.clamp(cameraOffset, 0, 10) * 5)
  95.         crosshair.Bottom.Position = UDim2.new(crosshair.Bottom.Position.X.Scale, 0, crosshair.Bottom.Position.Y.Scale, math.clamp(cameraOffset, 0, 10) * 5)
  96.         crosshair.Left.Position = UDim2.new(crosshair.Left.Position.X.Scale, -math.clamp(cameraOffset, 0, 10) * 5, crosshair.Left.Position.Y.Scale, 0)
  97.         crosshair.Right.Position = UDim2.new(crosshair.Right.Position.X.Scale, math.clamp(cameraOffset, 0, 10) * 5, crosshair.Right.Position.Y.Scale, 0)
  98.     end
  99. end)
  100.  
  101. --Server requests for gun
  102. remoteEvent.OnClientEvent:Connect(function(gunEquipped, instruction, p3)
  103.     if gunEquipped == gun then
  104.         --Add recoil
  105.         if instruction == "Recoil" then
  106.             cameraOffset += gunSettings["cameraRecoil"]
  107.             --Play reload animation
  108.         elseif instruction == "Reload" then
  109.             animations["reload"]:Play()
  110.         end
  111.  
  112.         --Adjust tracer to be smoother on client
  113.     elseif instruction == "Tracer" then
  114.  
  115.         local beamContainer = Instance.new("Part")
  116.         beamContainer.Name = "BEAM CONTAINER"
  117.         beamContainer.CanCollide = false
  118.         beamContainer.Transparency = 1
  119.         beamContainer.Anchored = true
  120.         beamContainer.Parent = workspace
  121.  
  122.         local tracer = Instance.new("Beam")
  123.         tracer.Color = ColorSequence.new{ColorSequenceKeypoint.new(0, Color3.fromRGB(255, 160, 40)), ColorSequenceKeypoint.new(1, Color3.fromRGB(255, 160, 40))}
  124.         tracer.FaceCamera = true
  125.         tracer.LightEmission = 5
  126.         local atch0, atch1 = Instance.new("Attachment"), Instance.new("Attachment")
  127.         atch0.Name, atch0.Name = "A0", "A1"
  128.         atch0.Parent, atch1.Parent = beamContainer, beamContainer
  129.         atch0.WorldPosition, atch1.WorldPosition = gunEquipped.ShootPart.Position, p3
  130.         tracer.Attachment0, tracer.Attachment1 = atch0, atch1
  131.         tracer.Width0, tracer.Width1 = 0.05, 0.05
  132.  
  133.         tracer.Parent = beamContainer
  134.         game:GetService("Debris"):AddItem(beamContainer, 0.1)
  135.     end
  136. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement