Machine Gun Firerate

Hi, I am making a U.S vs Japan game, and the machine gusn for the U.S Planes don’t seem to fire fast enough. It shoots very slowly despite being supposed to shoot around 20 rounds per second. Code:
Please help!!!

wait(7)
local model = script.Parent
local e = Instance.new('BodyGyro')
e.Parent = script.Parent.Engine
local engine = script.Parent.Engine
local function FireGuns(target)
	for v,Part in pairs(script.Parent:GetDescendants()) do
		if Part.Name == "MachineGun" then
			wait(0.05)
			local bullet = Instance.new("Part")
			bullet.Position = Part.Position
			bullet.CanCollide = false
			local v = Instance.new("BodyVelocity")
			bullet.Orientation = Part.Orientation
			bullet.Name = "AB"
			v.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
			v.Velocity = bullet.CFrame.LookVector * -300
			v.Parent = bullet
			bullet.Parent = workspace
			bullet.Size = Vector3.new(0.5,0.5,2)
			bullet.Material = "Neon"
			bullet.BrickColor = BrickColor.new("Bright blue")
			bullet.Touched:connect(function(part)
				local plane = part:FindFirstAncestor("USFighter")
				if not plane then
					bullet:Destroy()
				end
			end)
			delay(3,function() bullet:Destroy() end)
		end
	end
end
local function shoot(target)
	if target.JapPlane then
		if (script.Parent.USPlane.Position - target.JapPlane.Position).magnitude < 801 and target.Health.Value > 0 then
			FireGuns(target)
		end
	end
end
local function getTargets()
	local targets = workspace.americanTargets
	for v,t in pairs(workspace:GetDescendants()) do
		local enemyPlane = t:FindFirstAncestor("JapPlane")
		if enemyPlane and enemyPlane.Parent.Health.Value > 0 then
			enemyPlane.Parent.Parent = targets
			print(#targets:GetChildren())
		end
	end
	return targets
end
local function findNearestEnemy(targets)
	local target = nil
	local distance = nil
	local previousDistance = 99999
	for i,v in pairs(targets:GetChildren()) do
		if v:IsA("Model") and v.JapPlane and v.Health.Value > 0 then
			distance = (script.Parent.USPlane.Position - v.JapPlane.Position).magnitude
			if distance < previousDistance and v.Health.Value > 0 and (script.Parent.USPlane.Position - v.JapPlane.Position).magnitude < 1501 then
				previousDistance = distance
				target = v
			end
		end
	end
	return target
end
local function attack(target)
	if target and target.JapPlane and target.Health.Value > 0 then
		local origincframe = engine.BodyGyro.cframe
		local dir = (script.Parent.USPlane.Position - target.JapPlane.Position).unit
		local spawnPos = script.Parent.USPlane.Position
		local pos = spawnPos + dir 	
		engine:findFirstChild("BodyGyro").maxTorque = Vector3.new(10000,10000,10000)
		engine:findFirstChild("BodyGyro").cframe = CFrame.new(pos, pos+dir)
		shoot(target)
	end
end
local function patrolAi()
	local targets = getTargets()
	local target = findNearestEnemy(targets)
	if target and target.Health.Value > 0 then
		attack(target)
	end	
end
while true do
	wait(0.001)
	patrolAi()
end

Try setting all of the Properties of the bullet, then parenting it to workspace.

It’s probably a lag issue since you are creating each bullet from scratch each 1/20th of a second.

Try having a completed bullet Part with all the Properties set, then cloning it instead of creating it each time.

1 Like

@Scottifly Still didn’t work.

wait(7)
local model = script.Parent
local e = Instance.new('BodyGyro')
e.Parent = script.Parent.Engine
local engine = script.Parent.Engine
local function FireGuns(target)
	for v,Part in pairs(script.Parent:GetDescendants()) do
		if Part.Name == "MachineGun" then
			wait(0.05)
			local s = game:GetService("ServerStorage")
			local b = s.AB
			local bullet = b:Clone()
			bullet.Position = Part.Position
			bullet.Orientation = Part.Orientation
			bullet.Parent = workspace
			delay(3,function() bullet:Destroy() end)
		end
	end
end
local function shoot(target)
	if target.JapPlane then
		if (script.Parent.USPlane.Position - target.JapPlane.Position).magnitude < 801 and target.Health.Value > 0 then
			FireGuns(target)
		end
	end
end
local function getTargets()
	local targets = workspace.americanTargets
	for v,t in pairs(workspace:GetDescendants()) do
		local enemyPlane = t:FindFirstAncestor("JapPlane")
		if enemyPlane and enemyPlane.Parent.Health.Value > 0 then
			enemyPlane.Parent.Parent = targets
			print(#targets:GetChildren())
		end
	end
	return targets
end
local function findNearestEnemy(targets)
	local target = nil
	local distance = nil
	local previousDistance = 99999
	for i,v in pairs(targets:GetChildren()) do
		if v:IsA("Model") and v.JapPlane and v.Health.Value > 0 then
			distance = (script.Parent.USPlane.Position - v.JapPlane.Position).magnitude
			if distance < previousDistance and v.Health.Value > 0 and (script.Parent.USPlane.Position - v.JapPlane.Position).magnitude < 1501 then
				previousDistance = distance
				target = v
			end
		end
	end
	return target
end
local function attack(target)
	if target and target.JapPlane and target.Health.Value > 0 then
		local origincframe = engine.BodyGyro.cframe
		local dir = (script.Parent.USPlane.Position - target.JapPlane.Position).unit
		local spawnPos = script.Parent.USPlane.Position
		local pos = spawnPos + dir 	
		engine:findFirstChild("BodyGyro").maxTorque = Vector3.new(10000,10000,10000)
		engine:findFirstChild("BodyGyro").cframe = CFrame.new(pos, pos+dir)
		shoot(target)
	end
end
local function patrolAi()
	local targets = getTargets()
	local target = findNearestEnemy(targets)
	if target and target.Health.Value > 0 then
		attack(target)
	end	
end
while true do
	wait(0.001)
	patrolAi()
end

@Scottifly Hello?

It most likely is something with this piece of code. What is it? Just destroying the bullet after 3 seconds? If so, replace it with this:

game:GetService("Debris"):AddItem(bullet, 3)
2 Likes

@EternalEthel Thank you! I will try it. Can I ping you after to tell you results?

1 Like

No problem. Go for it, it should work!

@EternalEthel Hi. It still doesn’t shoot fast enough unfortunately, and for some reason it won’t update the direction without wait around 2 seconds.

wait(7)
local model = script.Parent
local e = Instance.new('BodyGyro')
e.Parent = script.Parent.Engine
local engine = script.Parent.Engine
local function FireGuns(target)
	for v,Part in pairs(script.Parent:GetChildren()) do
		if Part.Name == "MachineGun" then
			local s = game:GetService("ServerStorage")
			local b = s.AB
			local bullet = b:Clone()
			bullet.Position = Part.Position
			bullet.Orientation = Part.Orientation
			bullet.Parent = workspace
			game:GetService("Debris"):AddItem(bullet, 3)
			wait(0.05)
		end
	end
end

local function getTargets()
	local targets = workspace.americanTargets
	for v,t in pairs(workspace:GetDescendants()) do
		local enemyPlane = t:FindFirstAncestor("JapPlane")
		if enemyPlane and enemyPlane.Parent.Health.Value > 0 then
			enemyPlane.Parent.Parent = targets
			print(#targets:GetChildren())
		end
	end
	return targets
end
local function findNearestEnemy(targets)
	local target = nil
	local distance = nil
	local previousDistance = 99999
	for i,v in pairs(targets:GetChildren()) do
		if v:IsA("Model") and v.JapPlane and v.Health.Value > 0 then
			distance = (script.Parent.USPlane.Position - v.JapPlane.Position).magnitude
			if distance < previousDistance and v.Health.Value > 0 and (script.Parent.USPlane.Position - v.JapPlane.Position).magnitude < 1501 then
				previousDistance = distance
				target = v
			end
		end
	end
	return target
end
local function attack(target)
	if target and target.JapPlane and target.Health.Value > 0 then
		local origincframe = engine.BodyGyro.cframe
		local dir = (script.Parent.USPlane.Position - target.JapPlane.Position).unit
		local spawnPos = script.Parent.USPlane.Position
		local pos = spawnPos + dir 	
		engine:findFirstChild("BodyGyro").maxTorque = Vector3.new(10000,10000,10000)
		engine:findFirstChild("BodyGyro").cframe = CFrame.new(pos, pos+dir)
	end
end
local function shoot(target)
	if (script.Parent.USPlane.Position - target.JapPlane.Position).magnitude < 801 and target.Health.Value > 0 then
		attack(target)
		FireGuns(target)

	end
end
local function patrolAi()
	local targets = getTargets()
	local target = findNearestEnemy(targets)
	if target and target.Health.Value > 0 then
		attack(target)
		shoot(target)
	end	
end
while true do
	wait(0.001)
	patrolAi()
end
2 Likes

Hm. I can’t think of what else would be wrong, unfortunately, sorry!

1 Like

@EternalEthel Oh well. Thanks for trying!

1 Like