Spamming click on automatic gun fires way too fast, can't figure out why it's happening

Hello, recently I have been working on a Zombie Tower rework, and the gun system has been going very well, but there is just 1 issue. Holding left click is perfectly fine with an automatic gun, but when I spam left click the gun goes insane. Here is what happens:

Here is the code you may need:

UserInputService.InputBegan:Connect(function(InputObject, GameProcessedEvent)
	if not GameProcessedEvent then
		if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
			if not CanShoot then
				return
			end

			if Shooting then
				return
			end
			
			if ViewModel:FindFirstChild("AmmoValue", true).Value <= 0 then
				return
			end
			
			if ViewModel:FindFirstChild("AmmoValue", true).Value <= 0 and ViewModel:FindFirstChild("ChamberValue", true).Value <= 0 then
				return
			end
			
			local StartShoot = time()

			Shooting = true

			if GunValues[CurrentEquipped].IsAutomatic then
				repeat
					ViewModel:FindFirstChild("AmmoValue", true).Value -= 1
					
					task.spawn(RenderBullet)
					task.spawn(RecoilSetup)
					
					print(CurrentEquipped, "Casted a Bullet")
					
					local ShootAnimation = Animator:LoadAnimation(PresetShootAnimation)
					ShootAnimation:Play()
					
					task.wait(GunValues[CurrentEquipped].ShootCooldown)
				until not Shooting or not CanShoot or ViewModel:FindFirstChild("AmmoValue", true).Value <= 0
			else
				ViewModel:FindFirstChild("AmmoValue", true).Value -= 1
				
				task.spawn(RenderBullet)
				task.spawn(RecoilSetup)
				
				print(CurrentEquipped, "Casted a Bullet")

				task.wait(GunValues[CurrentEquipped].ShootCooldown)
			end
		end
		
		if InputObject.UserInputType == Enum.UserInputType.MouseButton2 then
			if Reloading then
				return
			end
			
			Aiming = true
		end
		
		if InputObject.UserInputType == Enum.UserInputType.Keyboard then
			if InputObject.KeyCode ~= Enum.KeyCode.R then
				return
			end
			
			if Shooting then
				return
			end
			
			if Reloading then
				return
			end
			
			if Aiming then
				return
			end
			
			if ViewModel:FindFirstChild("AmmoValue", true).Value > 0 then
				return
			end
			
			if ViewModel:FindFirstChild("ChamberValue", true).Value <= 0 then
				return
			end
			
			Reloading = true
			CanShoot = false
			
			task.spawn(function()
				task.wait(GunValues[CurrentEquipped].ReloadTime)
				
				Reloading = false
				CanShoot = true
				
				ViewModel:FindFirstChild("AmmoValue", true).Value = GunValues[CurrentEquipped].AmmoValue
				ViewModel:FindFirstChild("ChamberValue", true).Value -= GunValues[CurrentEquipped].AmmoValue
			end)
			
			--[[task.delay(GunValues[CurrentEquipped].ReloadTime, function()
				Reloading = false
				CanShoot = true
			end)--]]
			
			local ReloadAnimation = Animator:LoadAnimation(PresetReloadAnimation)
			ReloadAnimation:Play()
			
			local EquippedGun = ViewModel:FindFirstChild(CurrentEquipped)

			if EquippedGun then
				local MainHandle = EquippedGun:FindFirstChild("MainHandle", true)

				if MainHandle then
					task.spawn(function()
						task.wait(.85)

						local ReloadSound = MakeSound(GunValues[CurrentEquipped].ReloadSoundId, false, .51, 1.75, MainHandle)
						ReloadSound:Play()
					end)
				end
			end
		end
	end
end)

UserInputService.InputEnded:Connect(function(InputObject, GameProcessedEvent)
	if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
		if Reloading then
			return
		end
		
		Shooting = false
	end
	
	if InputObject.UserInputType == Enum.UserInputType.MouseButton2 then
		Aiming = false
	end
end)

Any help is appreciated, thanks!

2 Likes

I would use ticks, what I do for my game to counteract this and just make my guns better is this:

local lastFired = 0

local gunRPM = 60/toolSettings.gunRPM -- gunRPM would be like 200 or 700 

local function fire()
if ((tick() - (lastFired or 0)) < gunRPM ) then return end
 
lastFired = tick()


end

repeat fastWait()
fire()
until not firing

of course this doesn’t use the real values my code does but that’s the gist of it.

fastWait() is a module I use, you can just use task.wait()

1 Like

Is GunRPM like the gun shoot cooldown time? wait nevermind

1 Like

Figured it out, just had to do a bit of thinking. Thank you for your help though.

if IsOnAutomaticCooldown then
					return
				end
				
				repeat
					IsOnAutomaticCooldown = true
					
					ViewModel:FindFirstChild("AmmoValue", true).Value -= 1
					
					task.spawn(RenderBullet)
					task.spawn(RecoilSetup)
					
					print(CurrentEquipped, "Casted a Bullet")
					
					local ShootAnimation = Animator:LoadAnimation(PresetShootAnimation)
					ShootAnimation:Play()
					
					task.wait(GunValues[CurrentEquipped].ShootCooldown)
					
					IsOnAutomaticCooldown = false
				until not Shooting or not CanShoot or ViewModel:FindFirstChild("AmmoValue", true).Value <= 0
2 Likes