Advertisement
HowToRoblox

RocketLauncherClient

Aug 14th, 2022
1,142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.64 KB | None | 0 0
  1. --Wait for character to properly load
  2. wait(1)
  3. --Variables
  4. local remoteEvent = game.ReplicatedStorage:WaitForChild("RocketLauncherRE")
  5.  
  6. local mouse = game.Players.LocalPlayer:GetMouse()
  7.  
  8. local camera = workspace.CurrentCamera
  9. local cameraOffset = 0
  10.  
  11. local character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
  12. local humanoid = character:WaitForChild("Humanoid")
  13.  
  14. local tool = script.Parent
  15. local config = tool:WaitForChild("Settings")
  16.  
  17. local anims = {
  18.     holdAnim = humanoid:LoadAnimation(script:WaitForChild("HoldAnimation")),
  19.     reloadAnim = humanoid:LoadAnimation(script:WaitForChild("ReloadAnimation")),
  20.     shootAnim = humanoid:LoadAnimation(script:WaitForChild("ShootAnimation"))
  21. }
  22.  
  23. local canShoot = false
  24.  
  25.  
  26. --Equipping
  27. tool.Equipped:Connect(function()
  28.     remoteEvent:FireServer("ConnectM6D", tool)
  29.  
  30.     character.UpperTorso.RocketLauncherM6D.Part0 = character.UpperTorso
  31.     character.UpperTorso.RocketLauncherM6D.Part1 = tool.BodyAttach
  32.  
  33.     anims["holdAnim"]:Play()
  34.     canShoot = true
  35. end)
  36. --Unequipping
  37. tool.Unequipped:Connect(function()
  38.     canShoot = false
  39.     remoteEvent:FireServer("DisconnectM6D", tool)
  40.  
  41.     for i, anim in pairs(anims) do
  42.         anim:Stop()
  43.     end
  44. end)
  45.  
  46. --Shooting
  47. mouse.Button1Up:Connect(function()
  48.  
  49.     if canShoot then
  50.         canShoot = false
  51.  
  52.         anims["shootAnim"]:Play()
  53.  
  54.         remoteEvent:FireServer("Shoot", tool, mouse.Hit.Position)
  55.        
  56.         cameraOffset += 3
  57.        
  58.         --Rocket Jumping - remove these lines if you don't want this feature
  59.         local hrp = character.HumanoidRootPart
  60.         local velocity = -CFrame.new(tool.Rocket.Position, mouse.Hit.Position).LookVector * 100
  61.         hrp.Velocity = velocity
  62.         --
  63.     end
  64. end)
  65.  
  66. --Handle server requests to this client
  67. remoteEvent.OnClientEvent:Connect(function(instruction, rocketLauncher, cf, serverRocket)
  68.     --Play reload animation after shooting
  69.     if instruction == "reload" and rocketLauncher == tool then
  70.         anims["reloadAnim"]:Play()
  71.        
  72.     --Handle rocket on clients for smoother motion
  73.     elseif instruction == "shootRocket" then
  74.        
  75.         local newRocket = rocketLauncher.Rocket:Clone()
  76.         newRocket.Transparency = 0
  77.        
  78.         newRocket.CFrame = cf
  79.  
  80.         local speed = rocketLauncher.Settings.RocketSpeed.Value
  81.         local velocity = cf.LookVector * speed
  82.         newRocket.Velocity = velocity
  83.        
  84.         local trail = Instance.new("Trail")
  85.         trail.FaceCamera = true
  86.         trail.Transparency = NumberSequence.new{NumberSequenceKeypoint.new(0, 0.5), NumberSequenceKeypoint.new(1, 1)}
  87.         trail.Lifetime = 0.1
  88.        
  89.         local atch1 = newRocket.Attachment0:Clone()
  90.         atch1.Position = Vector3.new(0.2, 0, 0)
  91.         atch1.Parent = newRocket
  92.        
  93.         trail.Attachment0 = newRocket.Attachment0
  94.         trail.Attachment1 = atch1
  95.         trail.Parent = newRocket
  96.        
  97.         local whoosh = rocketLauncher.BodyAttach.WhooshSound:Clone()
  98.         whoosh.Looped = true
  99.         whoosh.Parent = newRocket
  100.         whoosh:Play()
  101.        
  102.         newRocket.Parent = workspace.RocketsContainer
  103.  
  104.         serverRocket.Destroying:Connect(function()
  105.             newRocket:Destroy()
  106.             camera.CameraType = Enum.CameraType.Custom
  107.         end)
  108.        
  109.         --Remove cooldown when reloading is finished
  110.     elseif instruction == "cooldown" then
  111.         canShoot = true
  112.     end
  113. end)
  114.  
  115. --Event that fires every frame
  116. game:GetService("RunService").Stepped:Connect(function()
  117.     --Apply recoil to camera
  118.     if tool.Parent == character then
  119.         camera.CFrame = camera.CFrame:Lerp(camera.CFrame * CFrame.Angles(cameraOffset / 30, 0, 0), 0.4)
  120.         cameraOffset = math.clamp(cameraOffset - 0.1, 0, math.huge)
  121.     end
  122.    
  123.     --Make rockets face the direction they are falling
  124.     for i, rocket in pairs(workspace.RocketsContainer:GetChildren()) do
  125.         rocket.CFrame = CFrame.new(rocket.Position, rocket.Velocity + rocket.Position)
  126.     end
  127. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement