Hello! In this tutorial I will teach you how to make a rocket launcher, lets get started. First you will need a rocket launcher parts/union/part, and a rocket part/union.
Setting Up Needed Items
After you have those you want to name your rocket launcher “Handle” (or if your rocket launcher is multiple parts then name one of them “Handle”) and put it into a tool. For the rocket, you want to put it into replicated storage and name it “Rocket.” Then put a LocalScript into your tool, a Script into your tool,a script into ServerScriptService, and RemoteEvent into your tool. Name the RemoteEvent “Fire” and put your tool into StarterPack.
Next insert a RemoteEvent into ReplicatedStorage and name it the same as the RemoteEvent in your tool. Also you need to insert a part into your tool and call it “RocketHole”, put it at the front of your rocket launcher, adjust it, and weld it to the main part of your rocket launcher. After that insert a LocalScript into StarterPlayerScripts and name it whatever you like (i will go over why we need that in a little bit).
Now that we got everything set up, we can begin scripting the tool.
Scripting The Tool
We are going to script the LocalScript first of course, first we need to setup some variables. One for the tool, one for the fire event in the tools, one for cool down time, and one for debounce. So we will write this in the LocalScript.
local tool = script.Parent
local fireEvent = tool:WaitForChild("Fire")
coolDown = 3 -- Can change value if its is a number
canFire = true
Now we have got all of our needed variables, we can now script the click function. Write this after all of the needed variables.
tool.Equipped:Connect(function(mouse)
mouse.Button1Down:Connect(function()
end)
end)
After that we now script the rest of it with the debounce and cool down. So this is how the shoot function should look
tool.Equipped:Connect(function(mouse)
mouse.Button1Down:Connect(function()
if canFire then
fireEvent:FireServer(mouse.Hit.Position, coolDown)
canFire = false
wait(coolDown)
canFire = true
end
end)
end)
When we fire the fire event and use mouse.Hit.Position, we will use that to make the rocket face the mouse’s hit. But that is all we need to do in the LocalScript, we can now script the server script. First we will put these two variables.
local tool = script.Parent
local fireEvent = tool:WaitForChild("Fire")
After that we will script this.
fireEvent.OnServerEvent:Connect(function(plr, mouseHit, coolDown)
game.ReplicatedStorage.Fire:FireAllClients(plr, mouseHit,tool)
end)
Now i will explain why we need that LocalScript in StarterPlayerScripts. We are going to be using that LocalScript to clone the rocket, because if i did that in a server script there would be delay in the fire (the rocket would be frozen for a few seconds). So when the remote event in ReplicatedStorage gets locally fired, the LocalScript will generate the rocket. So now lets write the function that will clone the rocket in the LocalScript inside StarterPlayerScripts.
Generating The Rocket
game.ReplicatedStorage.Fire.OnClientEvent:Connect(function(plr, mouseHit, tool)
local rocket = game.ReplicatedStorage.Rocket:Clone()
rocket.Parent = workspace
rocket.CFrame = CFrame.new(tool.RocketHole.Position, mouseHit)
end)
Now when we use this the rocket will be at the part you inserted into your tool named “RocketHole” and face the direction of the mouse’s hit. But it doesn’t go anywhere, the reason for that is because we never setup code for it to be able to move to the mouse hit. To do that we have to write this line of code.
local bv = Instance.new("BodyVelocity", rocket)
bv.Velocity = CFrame.new(rocket.Position, mouseHit).LookVector * 100
If you want the rocket to go faster or slower, just adjust the number that the lookvector is multiplied by.
So far the code should look like this.
game.ReplicatedStorage.Fire.OnClientEvent:Connect(function(plr, mouseHit, tool)
local rocket = game.ReplicatedStorage.Rocket:Clone()
rocket.Parent = workspace
rocket.CFrame = CFrame.new(tool.RocketHole.Position, mouseHit)
local bv = Instance.new("BodyVelocity", rocket)
bv.Velocity = CFrame.new(rocket.Position, mouseHit).LookVector * 100
end)
There we have now got the rocket to fire toward your mouse. But what is a rocket launcher if its rockets don’t actually explode? So we need to make a touched function. For now we write this.
rocket.Touched:Connect(function(hit)
end)
After that we need to make it so that when the rocket touches a part it deletes, so it doesn’t go through walls. So the touched function should look like this.
rocket.Touched:Connect(function(hit)
if hit:IsA("BasePart") then
if hit.Parent ~= tool then
rocket:Destroy()
end
end
wait(3)
rocket:Destroy()
end)
Now it is explaining time, when the rocket is hit we check if it is a BasePart, and if it is we check if what ever it is isn’t a descendant of the rocket launcher tool. And if it isn’t, it will destroy the rocket. And we wait three seconds, if the rocket doesn’t hit anything in three seconds, it still gets destroyed. But if it does hit a part that isn’t in the rocket launcher tool, we will fire the RemoteEvent “Fire” in Replicated Storage. So this is what the finished script looks like.
game.ReplicatedStorage.Fire.OnClientEvent:Connect(function(plr, mouseHit, tool)
local rocket = game.ReplicatedStorage.Rocket:Clone()
rocket.Parent = workspace
rocket.CFrame = CFrame.new(tool.RocketHole.Position, mouseHit)
local bv = Instance.new("BodyVelocity", rocket)
bv.Velocity = CFrame.new(rocket.Position, mouseHit).LookVector * 100
rocket.Touched:Connect(function(hit)
if hit:IsA("BasePart") then
if hit.Parent ~= tool then
rocket:Destroy()
game.ReplicatedStorage.Fire:FireServer(rocket.Position, mouseHit)
end
end
wait(3)
rocket:Destroy()
end)
end)
Now we can make the explosion happen after the rocket has hit something.
Making The Explosions
So now we can go to the script we put into ServerScriptService and write this.
game.ReplicatedStorage.Fire.OnServerEvent:Connect(function(plr, rocketPos, mouseHit)
local explosionPart = Instance.new("Part", workspace)
explosionPart.Anchored = true
explosionPart.CanCollide = false
explosionPart.Transparency = 1
explosionPart.Position = rocketPos
end)
You maybe wondering “Why are we making a part?” And the answer to that is because we are gonna implement sound into that part. So insert an explosion sound effect into ReplicatedStorage and call it “ExplosionSound.” Now this is what the code should look like so far.
game.ReplicatedStorage.Fire.OnServerEvent:Connect(function(plr, rocketPos, mouseHit)
local explosionPart = Instance.new("Part", workspace)
explosionPart.Anchored = true
explosionPart.CanCollide = false
explosionPart.Transparency = 1
explosionPart.Position = rocketPos
local expoSound = game.ReplicatedStorage.ExplosionSound:Clone()
expoSound.Parent = explosionPart
expoSound:Play()
end)
There we have got an explosion sound, but we still have no explosions, to fix that we write this.
local explosion = Instance.new("Explosion", workspace)
explosion.Position = explosionPart.Position
wait(3)
explosionPart:Destroy()
There we have made explosions, and we also don’t want to strain the server with the already used explosion part. So we just destroy it after the explosion and sound is done. So this is what the script should look like.
game.ReplicatedStorage.Fire.OnServerEvent:Connect(function(plr, rocketPos, mouseHit)
local explosionPart = Instance.new("Part", workspace)
explosionPart.Anchored = true
explosionPart.CanCollide = false
explosionPart.Transparency = 1
explosionPart.Position = rocketPos
local expoSound = game.ReplicatedStorage.ExplosionSound:Clone()
expoSound.Parent = explosionPart
expoSound:Play()
local explosion = Instance.new("Explosion", workspace)
explosion.Position = explosionPart.Position
wait(3)
explosionPart:Destroy()
end)
We have made the explosions, now we can make an animation and one more detail. So insert an animation into your tool, put your animation id in it, and name it “Shoot.”
So we go back into the Script inside of your tool and write this.
local shootAnim = tool.Parent.Humanoid:LoadAnimation((tool.Shoot))
shootAnim:Play()
For now this is what the script will look like
local tool = script.Parent
local fireEvent = tool:WaitForChild("Fire")
fireEvent.OnServerEvent:Connect(function(plr, mouseHit, coolDown)
game.ReplicatedStorage.Fire:FireAllClients(plr, mouseHit,tool)
local shootAnim = tool.Parent.Humanoid:LoadAnimation((tool.Shoot))
shootAnim:Play()
end)
Now that we have got it to play a shoot animation, we can now add the last detail. If your rocket launcher has a hole that you can see through like this.
Then you can add this one last detail from this tutorial. If a player is in front of you they will be able to see inside of your rocket, and they will see that there is no rocket inside your rocket launcher. But when you shoot, one will magically come out of nowhere. So if you want to fix this, duplicate your rocket from Replicated Storage and put it into your rocket launcher tool (don’t change its name), adjust it to the center of your rocket launcher hole, and weld it to the rocket launcher. So now we go back into the server script that is in your rocket launcher tool and write this.
tool.Rocket.Transparency = 1
wait(coolDown)
tool.Rocket.Transparency = 0
So whenever you shoot the rocket inside of your rocket launcher goes invisible, and when your cool down time has finished it will be visible again. so this is what the finished script should look like.
local tool = script.Parent
local fireEvent = tool:WaitForChild("Fire")
fireEvent.OnServerEvent:Connect(function(plr, mouseHit, coolDown)
game.ReplicatedStorage.Fire:FireAllClients(plr, mouseHit,tool)
local shootAnim = tool.Parent.Humanoid:LoadAnimation((tool.Shoot))
shootAnim:Play()
tool.Rocket.Transparency = 1
wait(coolDown)
tool.Rocket.Transparency = 0
end)
Well that is the end of the tutorial, i hope you found this helpful! If i misspelled anything, did anything wrong, something isn’t working, ect. Please tell me, and thanks for reading this tutorial.
EDIT
02Alien Pointed out using Debris to remove the rocket and explosion part which is a better idea. Here is the updated ServerScriptService script, and rocket generation script.
ServerScriptServiceScript:
local debris = game:GetService("Debris")
game.ReplicatedStorage.Fire.OnServerEvent:Connect(function(plr, rocketPos, mouseHit)
local explosionPart = Instance.new("Part", workspace)
explosionPart.Anchored = true
explosionPart.CanCollide = false
explosionPart.Transparency = 1
explosionPart.Position = rocketPos
local expoSound = game.ReplicatedStorage.ExplosionSound:Clone()
expoSound.Parent = explosionPart
expoSound:Play()
local explosion = Instance.new("Explosion", workspace)
explosion.Position = explosionPart.Position
debris:AddItem(explosionPart, 3)
end)
Rocket Generation Script:
local debris = game:GetService("Debris")
game.ReplicatedStorage.Fire.OnClientEvent:Connect(function(plr, mouseHit, tool)
local rocket = game.ReplicatedStorage.Rocket:Clone()
rocket.Parent = workspace
rocket.CFrame = CFrame.new(tool.RocketHole.Position, mouseHit)
local bv = Instance.new("BodyVelocity", rocket)
bv.Velocity = CFrame.new(rocket.Position, mouseHit).LookVector * 100
rocket.Touched:Connect(function(hit)
if hit:IsA("BasePart") then
if hit.Parent ~= tool then
rocket:Destroy()
game.ReplicatedStorage.Fire:FireServer(rocket.Position, mouseHit)
end
end
debris:AddItem(rocket, 3)
end)
end)
Finished Product: Rocket Launcher - Roblox