Advertisement
HowToRoblox

RocketLauncherServer

Aug 14th, 2022
1,811
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.10 KB | None | 0 0
  1. local tweenService = game:GetService("TweenService")
  2.  
  3. local remoteEvent = game.ReplicatedStorage:WaitForChild("RocketLauncherRE")
  4.  
  5. local cooldown = {}
  6.  
  7. --Create container for rockets
  8. local rocketsContainer = Instance.new("Folder")
  9. rocketsContainer.Name = "RocketsContainer"
  10. rocketsContainer.Parent = workspace
  11.  
  12. --Create connection when character spawns
  13. game.Players.PlayerAdded:Connect(function(player)          
  14.     player.CharacterAdded:Connect(function(character)  
  15.  
  16.         local motor6d = Instance.new("Motor6D")
  17.         motor6d.Name = "RocketLauncherM6D"
  18.         motor6d.Part0 = character.UpperTorso
  19.         motor6d.Parent = character.UpperTorso
  20.     end)
  21. end)
  22.  
  23.  
  24. --Handle client requests for gun
  25. remoteEvent.OnServerEvent:Connect(function(player, instruction, rocketLauncher, mousePos)
  26.    
  27.     if player.Character and rocketLauncher and rocketLauncher.Parent == player.Character then
  28.         --Connect gun to character's torso when equipped
  29.         if instruction == "ConnectM6D" then
  30.             player.Character.UpperTorso.RocketLauncherM6D.Part1 = rocketLauncher.BodyAttach
  31.            
  32.             if not rocketLauncher.Rocket:FindFirstChild("Attachment0") then
  33.                 local atch0 = Instance.new("Attachment")
  34.                 atch0.Name = "Attachment0"
  35.                 atch0.Parent = rocketLauncher.Rocket
  36.             end
  37.            
  38.         --Disconnect gun from character's torso when unequipped
  39.         elseif instruction == "DisconnectM6D" then
  40.             player.Character.UpperTorso.RocketLauncherM6D.Part1 = nil
  41.            
  42.         --Fire a rocket when player clicks
  43.         elseif instruction == "Shoot" then
  44.             if not cooldown[player] then
  45.                 cooldown[player] = true
  46.                
  47.                 rocketLauncher.Rocket.Transparency = 1
  48.                
  49.                 local newRocket = rocketLauncher.Rocket:Clone()
  50.  
  51.                 local cf = CFrame.new(newRocket.Position, mousePos)
  52.                 newRocket.CFrame = cf
  53.                
  54.                 local speed = rocketLauncher.Settings.RocketSpeed.Value
  55.                 local velocity = cf.LookVector * speed
  56.                 newRocket.Velocity = velocity
  57.                
  58.                 rocketLauncher.BodyAttach.ShootSound:Play()
  59.  
  60.                 newRocket.Touched:Connect(function(hit)
  61.                     local explosionPos = newRocket.Position
  62.                    
  63.                     if not hit:IsDescendantOf(player.Character) then
  64.                        
  65.                         local explosionSound = rocketLauncher.BodyAttach.ExplosionSound:Clone()
  66.                        
  67.                         newRocket:Destroy()
  68.                        
  69.                         local explosion = Instance.new("Explosion")
  70.                         explosion.Position = explosionPos
  71.                         explosion.Parent = workspace
  72.                        
  73.                         local atch = Instance.new("Attachment")
  74.                         atch.WorldPosition = explosionPos
  75.                         explosionSound.Parent = atch
  76.                         atch.Parent = workspace.Terrain
  77.                         explosionSound:Play()
  78.  
  79.                         explosionSound.Stopped:Connect(function()
  80.                             atch:Destroy()
  81.                         end)
  82.                     end
  83.                 end)
  84.                
  85.                 newRocket.Parent = workspace
  86.                
  87.                 remoteEvent:FireAllClients("shootRocket", rocketLauncher, cf, newRocket)
  88.                 remoteEvent:FireClient(player, "reload", rocketLauncher)
  89.                
  90.                 wait(0.5)
  91.                 rocketLauncher.Rocket.Transparency = 0
  92.                 wait(0.3)
  93.                 rocketLauncher.BodyAttach.ReloadSound:Play()
  94.                
  95.                 wait(rocketLauncher.Settings.ReloadTime.Value - 0.5 - 0.3)
  96.                 cooldown[player] = nil
  97.                 remoteEvent:FireClient(player, "cooldown")
  98.             end
  99.         end
  100.     end
  101. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement