Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Automatic gun code shooting multiple times?

I am trying to make an M4A1, an automatic gun, but the code that shoots the bullets will shoot bullets twice as fast if I double click and hold down on the second click. Why? Am I not setting variables correctly? Is there a different method of auto firing I should use?

Below is a video of the problem. The first half is shooting normally, the second is when I double click and hold:

https://www.youtube.com/watch?v=o-C6Krr9GkM&feature=youtu.be

And here is the code (Ignore commented out code):

mouse.Button1Down:connect(function()	
		mouseDown = true
		repeat
			if mouseDown and ammo.Value > 0 and not isReloading and player.Character.Humanoid.Health > 0 then
				ammo.Value = ammo.Value - 1				

--				if ADS then
--					xOffset = aimOffset[math.random(1, #aimOffset)]
--					yOffset = aimOffset[math.random(1, #aimOffset)]
--				elseif not ADS then
--					xOffset = NoAimOffset[math.random(1, #NoAimOffset)]
--					yOffset = NoAimOffset[math.random(1, #NoAimOffset)]
--				end
--				
--				local mousePoint = mouse.Hit.p
--				
--				local xRandomAxis = math.random(1, 2)
--				local yRandomAxis = math.random(1, 2)
--				
--				if xRandomAxis == 1 then
--					xOffset = -xOffset
--				else
--					xOffset = xOffset
--				end
--				
--				if yRandomAxis == 1 then
--					yOffset = -yOffset
--				else
--					yOffset = yOffset
--				end
				
				local ray = Ray.new(tool.Barrel.CFrame.p, (mousePoint + Vector3.new(xOffset, yOffset, 0) - tool.Barrel.CFrame.p).unit * 300)
				local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
					
				local beam = Instance.new("Part", workspace)
				beam.BrickColor = BrickColor.new("Dark stone grey")
				beam.FormFactor = "Custom"
				beam.Transparency = 0.5
				beam.Anchored = true
				beam.Locked = true
				beam.CanCollide = false
					
				local distance = (tool.Barrel.CFrame.p - position).magnitude
				beam.Size = Vector3.new(0.1, 0.1, distance)
				beam.CFrame = CFrame.new(tool.Barrel.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
					
				game:GetService("Debris"):AddItem(beam, 0.1)
					
				script.Parent.Shoot:Play()
					
--				if part then
--					local humanoid = part.Parent:FindFirstChild("Humanoid")
--					 
--					if not humanoid then
--						humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
--					end
--					 
--					if humanoid then
--						player.PlayerGui.Hit:Play()
--						humanoid:TakeDamage(7)
--					end
--				end
				
				wait(0.2)
			else
				wait(0.2)
			end
		until mouseDown == false

1 answer


local Ready = true

Mouse.Button1Down:Connect(function()

while Ready == true and ammo.Value > 0 and  not isReloading do
Ready = false
-- Code here, use a while true do loop rather than a repeat. 
Ready = true -- AFTER THE GUN HAS FIRED AND YOU HAVE WAITED THE RATE OF FIRE 
end

end)

Mouse.Button1Up:Connect(function()

Ready = false

end)


2 comments

0

I tried it unfortunately it did not work.

0

I edited my answer

This discussion is preserved as it was. New answers, comments, and votes are closed.