Need help with tank fire code

  1. What do you want to achieve? I want the missile to call the touch event

  2. What is the issue? Missile just flies through objects and doesn’t call touch event

  3. What solutions have you tried so far? Tried changing my code, couldn’t find somethink helpful enough on forum

I am developing tank war game and I created missile shooting system, but I don’t know how to make it explode(touch the enemy tank and then explode), mainly because of touch event isn’t happening and I don’t know what I did wrong. I am newbie(like 3 days of programming, no docs, no ready examples) so my code can be bad.

Here is the code:

local mg_end = turret.Gun_57mm.MG_End
local shell = turret.Gun_57mm.LoadSystem.Shell57mm

local function fire()
	print("I fired")
	local shell_sp = turret.Gun_57mm.MG_End.CFrame * Vector3.new(0, 0, 3)
	local shell_velocity = -4
	
	local missile = shell:Clone()
	missile.BTWeld:Remove()
	missile.Position = shell_sp
	missile.Parent = game.Workspace
	missile.CanCollide = false
	while missile.Parent ~= nil do
		missile.CFrame = missile.CFrame * CFrame.new(shell_velocity, 0, 0)
		wait(0.00001)
	end
	local function missileTouch(hit)
		local am_explosion = Instance.new("Explosion")
		am_explosion.BlastRadius = 8
		am_explosion.BlastPressure = 500000
		am_explosion.Position = missile.Position
		am_explosion.Parent = game.Workspace
		missile.Parent = nil
	end
	while wait(3) do
		wait(.1)
		missile.Touched:Connect(function()
			print("HA I TOUCHED IT")
		end)
		missile.Touched:Connect(missileTouch())
	end
end

uis.InputBegan:Connect(function(input, gameProcessedEvent, player)
	if input.KeyCode == Enum.KeyCode.E and gun_seat:GetAttribute("isOccupied") == 1 then
		fire()
	end
end)
2 Likes

Have you made sure that CanTouch And CanQuery are on in the missile part?

idk what is CanQuery, but I enabled it with code and it didn’t make any difference

Hey, now it’s working, but it’s just exploding hitting air

Disable can touch on the turret part.

That may be the script thinking it has hit something but it was probably the Missile model, so you probably want to check that

1 Like

(Use an if statement to check)

Had any luck? Was it the model?

i still don’t know what to do. it didn’t even listen the touch. just I got the thing wrong, so the native problem is still actual(
And thx for caring

Its no problem, you never passed in the hit argument to the function
do this:

local mg_end = turret.Gun_57mm.MG_End
local shell = turret.Gun_57mm.LoadSystem.Shell57mm

local function fire()
	print("I fired")
	local shell_sp = turret.Gun_57mm.MG_End.CFrame * Vector3.new(0, 0, 3)
	local shell_velocity = -4
	
	local missile = shell:Clone()
	missile.BTWeld:Remove()
	missile.Position = shell_sp
	missile.Parent = game.Workspace
	missile.CanCollide = false
	while missile.Parent ~= nil do
		missile.CFrame = missile.CFrame * CFrame.new(shell_velocity, 0, 0)
		wait(0.00001)
	end
	local function missileTouch(hit)
		local am_explosion = Instance.new("Explosion")
		am_explosion.BlastRadius = 8
		am_explosion.BlastPressure = 500000
		am_explosion.Position = missile.Position
		am_explosion.Parent = game.Workspace
		missile.Parent = nil
	end
	while wait(3) do
		wait(.1)
		missile.Touched:Connect(function(hit)
			print("HA I TOUCHED IT")
		end)
		missile.Touched:Connect(function(hit)
		missileTouch(hit)
		end)
	end
end

uis.InputBegan:Connect(function(input, gameProcessedEvent, player)
	if input.KeyCode == Enum.KeyCode.E and gun_seat:GetAttribute("isOccupied") == 1 then
		fire()
	end
end)
1 Like