Rocket Launcher Script

Hello, so I’m having three problems with this script

  1. The explosion won’t explode unless it hits the ground but won’t if it hits a player/dummy

  2. The ammo gui doesn’t change to the current ammo

  3. I don’t want the player to die to their own rocket I can’t figure out how to fix that though

Client Side

local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local debounce = true
local ammo = 3
local ammogui = game.StarterGui["Ammo and Elims"].Ammo



tool.Activated:Connect(function()
	if ammo ~= 0 then
		local humanoid = tool.Parent:FindFirstChildWhichIsA("Humanoid")
	if humanoid and debounce == true then
		debounce = false
		local position = mouse.Hit.Position
			tool["Create Rocket"]:FireServer(position)
			ammo = ammo - 1
			
			
		task.wait(2)
		debounce =  true
			
		if ammo == 0 then
			ammogui.Text = "Reloading"
			wait(3)
			ammo = 3
			ammogui.Text = ammo
			end
		end
	end
end)


Server Side

local tool = script.Parent
local handle = tool:WaitForChild("Handle")


tool["Create Rocket"].OnServerEvent:Connect(function(player, position)
	
	local rocket = Instance.new("Part")
			rocket.Parent = workspace
			rocket.CFrame = CFrame.new(handle.Position + Vector3.new(0, 1, 0), position)
			rocket.Size = Vector3.new(1, 1, 2)
			rocket.BrickColor = BrickColor.new("Really red")
	
	
	local velocity = Instance.new("BodyVelocity")
			velocity.Parent = rocket
			velocity.Velocity = (position - handle.Position).Unit * 50
			velocity.MaxForce = Vector3.new("inf", "inf", "inf")
	
	rocket.Touched:Connect(function(hit)
		
		
		if hit ~= handle and not tool.Parent:FindFirstChild(hit.Name) then
				
				local explosion = Instance.new("Explosion")
				
				
			if explosion.Hit == player then
				explosion = nil
				
			else
				
				explosion.Parent = workspace
				explosion.Position = rocket.Position
				rocket:Destroy()
			end
			
			end
		end)
	end)

What happens

robloxapp-20220919-1428026.wmv (3.8 MB)

Pic of my explorer

Please help thanks!!!

Are you seeing any output in the console when you run out of Ammo? Also, is reloading ammo working as expected?

Yes it says when its reloading and the ammo works correctly. Also I have no errors.

1 Like

You setting the Text property incorrectly.

Text is a property of the TextLabel in ammogui.
You can set it like this:

ammogui.TextLabel.Text = ammo

Also, you are not updating the Text with the number of ammo each time you fire the rocket launcher.

2 Likes

Thanks. Do you know how to make the rocket explode when it hits the player that my second most important problem?

1 Like

Also I just tested it but i doesn’t work it says text label is not a valid memeber of starter gui.ammo and elims.ammo

1 Like

Is AmmoGui the TextLabel or the ScreenGUI?

The text label (char limit) i need more words lol

This should fix the ui bug

local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local debounce = true
local ammo = 3
local ammogui = player.PlayerGui:WaitForChild("Ammo and Elims").Ammo

tool.Activated:Connect(function()
	if ammo ~= 0 then
		local humanoid = tool.Parent:FindFirstChildWhichIsA("Humanoid")
		if humanoid and debounce == true then
			debounce = false
			local position = mouse.Hit.Position
			tool["Create Rocket"]:FireServer(position)
			ammo -= 1 
			ammogui.Text = ammo
			task.wait(2)
			debounce =  true
		end
	end
	if ammo == 0 then
		ammogui.Text = "Reloading"
		task.wait(3)
		ammo = 3
		ammogui.Text = ammo
	end
end)
1 Like

K ill check it out whe. I get on computer

Explosion.Hit==player

That part does not make sense to me. Hit is an event so you need to connect a function to it.

Check this out:

Try simplifying the code until it works, or make a breakpoint to step through the code and see what happens.

Yeah I know this didn’t make sense I was just playing with stuff trying to fix it myself.

Issue number 1:

Explosion.Hit is an event, not an Object Value. Issue 3’s solution fixes this.

Issue number 2:

for issue number 2, instead of relying on guis to see how much ammo the launcher has, i would use a bool value instead, subtract the value on the server and then use Instance:GetPropertyChangedSignal to detect when the rocket ammo has changed on the client so that the amount can be displayed.

Issue 3:

Overhaul your explosion system. Instead of using roblox’s default explosion functionality to kill players/npc’s, you can just create your own custom one without having to get rid of the explosion item.
You can simply set the blast radius to 0, then when the explosion happens, loop through everything that is intended to explode and then check its distance with magnitude. This is a commonly used method amongst developers.

If this fixed your problems mark this as a solution. :slight_smile:

2 Likes

I’m not sure how to do the third one but I will try or look up a tutorial thanks

the links i included in issue 3 should help

1 Like