Machine Gun debounce Is breaking.(Video)

I’m trying to make a machine gun where you can tap fire and hold down mouse to fire at the same speed without having to spam click. but when you spam left mouse really fast it start to spawn 2 at a time and sometimes. I have a video to demonstrate.

this is a server script inside the tool


local CanFire = true
local Firing = false
local AllowedClick = true
script.Parent.Equipped:Connect(function()
	Char = script.Parent.Parent
end)


script.Parent.Activated:Connect(function()
		if Firing == false and CanFire == true then 
			CanFire = false
			Firing = true
		
			repeat 	local Bullet = game.ReplicatedStorage.Bullets.BulletL1:Clone()
			Bullet.CFrame = CFrame.new(script.Parent.Tip.Position)
			Bullet.Parent = game.Workspace.SpawnedBalls
			
			local BulletDamage = Instance.new("IntValue", Bullet)
			BulletDamage.Name = "BulletDamage"
			BulletDamage.Value = Char.Stats.BulletDamage.Value
			
			local CharName = Instance.new("StringValue", Bullet)
			CharName.Name = "CharName"
			CharName.Value = Char.Name
			
			local BulletHit = Instance.new("BoolValue", Bullet)
			BulletHit.Name = "BulletHit"
			
			local BodyVelocity = Instance.new("BodyVelocity",Bullet)
			BodyVelocity.Velocity = Char.HumanoidRootPart.CFrame.lookVector * Char.Stats.BulletSpeed.Value 
			BodyVelocity.maxForce = Vector3.new(math.huge,math.huge,math.huge)
			
			game:GetService("Debris"):AddItem(Bullet,3)
			
			script.Parent.Color:FireClient(game.Players[Char.Name], Bullet)
			
			wait(Char.Stats.ReloadSpeed.Value*.5)
			local Bullet2 = game.ReplicatedStorage.Bullets.BulletL1:Clone()
			Bullet2.CFrame = CFrame.new(script.Parent.Tip2.Position)
			Bullet2.Parent = game.Workspace.SpawnedBalls
			
			local BulletDamage = Instance.new("IntValue", Bullet2)
			BulletDamage.Name = "BulletDamage"
			BulletDamage.Value = Char.Stats.BulletDamage.Value
			
			local CharName = Instance.new("StringValue", Bullet2)
			CharName.Name = "CharName"
			CharName.Value = Char.Name
			
			local BulletHit = Instance.new("BoolValue", Bullet2)
			BulletHit.Name = "BulletHit"
			
			local BodyVelocity = Instance.new("BodyVelocity",Bullet2)
			BodyVelocity.Velocity = Char.HumanoidRootPart.CFrame.lookVector * Char.Stats.BulletSpeed.Value 
			BodyVelocity.maxForce = Vector3.new(math.huge,math.huge,math.huge)
			
			game:GetService("Debris"):AddItem(Bullet2,3)
			
			script.Parent.Color:FireClient(game.Players[Char.Name], Bullet2)
			
			
			
			Bullet.Touched:connect(function(Hit)
				if Hit.Parent ~= Char and Hit.Parent:FindFirstChild("Humanoid") then
	
					Hit.Parent.Humanoid:TakeDamage(script.Parent.Parent.Stats.BodyDamage.Value)
					Bullet:Destroy()
				elseif  Hit.Parent.Name ~= script.Parent.Name and not Hit:IsDescendantOf(Char)and Hit.Parent.Name ~= game.Workspace.SpawnedBalls.Name then
					--hitsomthing else
				end 
			end)
			game.ReplicatedStorage.RemoteFunctions.Shoot:FireClient(game.Players[Char.Name])
			Bullet2.Touched:connect(function(Hit)
				if Hit.Parent ~= Char and Hit.Parent:FindFirstChild("Humanoid") then
	
					Hit.Parent.Humanoid:TakeDamage(script.Parent.Parent.Stats.BodyDamage.Value)
					Bullet:Destroy()
				elseif  Hit.Parent.Name ~= script.Parent.Name and not Hit:IsDescendantOf(Char)and Hit.Parent.Name ~= game.Workspace.SpawnedBalls.Name then
					--hitsomthing else
				end 
			end)
			game.ReplicatedStorage.RemoteFunctions.Shoot:FireClient(game.Players[Char.Name])
			wait(Char.Stats.ReloadSpeed.Value*.5)
				
			until Firing == false
		end
end)

script.Parent.Deactivated:Connect(function()
	if Firing == true then
		Firing = false
		wait(Char.Stats.ReloadSpeed.Value*.5)
		CanFire = true
	end
end)

So there are a lot of pretty bad things in your code that you ought to fix but in the name of slapping a bandaid on it here is what you should do.

Instead of having a function that yields a certain amount of time every time the mouse comes up you ought to throw a check in the activated method to see if that amount of time has passed. For example


local LastFired = tick() - Char.Stats.ReloadSpeed.Value*.5

script.Parent.Activated:Connect(function()
    if tick() - LastFired > Char.Stats.ReloadSpeed.Value*.5 then
    	LastFired = tick();
    	--Other things here
    end
end)

what do you think I should do here when it sets the local LastFired = tick() - Char.Stats.ReloadSpeed.Value*.5 it says char is a null value bc it is in the backpack when it’s trying to set char.

Put it inside of the script.Parent.Equipped:Connect(function()

Edit: Underneath where you defined char.

alright so still does same exact thing here is the code back


local CanFire = true
local Firing = false

script.Parent.Equipped:Connect(function()
   Char = script.Parent.Parent
   LastFired = tick() - Char.Stats.ReloadSpeed.Value*.5
end)


script.Parent.Activated:Connect(function()
   	if Firing == false and CanFire == true then 
   		if tick() - LastFired > Char.Stats.ReloadSpeed.Value*.5 then
   		LastFired = tick();
   
   		CanFire = false
   		Firing = true
   	
   		repeat 	local Bullet = game.ReplicatedStorage.Bullets.BulletL1:Clone()
   		Bullet.CFrame = CFrame.new(script.Parent.Tip.Position)
   		Bullet.Parent = game.Workspace.SpawnedBalls
   		
   		local BulletDamage = Instance.new("IntValue", Bullet)
   		BulletDamage.Name = "BulletDamage"
   		BulletDamage.Value = Char.Stats.BulletDamage.Value
   		
   		local CharName = Instance.new("StringValue", Bullet)
   		CharName.Name = "CharName"
   		CharName.Value = Char.Name
   		
   		local BulletHit = Instance.new("BoolValue", Bullet)
   		BulletHit.Name = "BulletHit"
   		
   		local BodyVelocity = Instance.new("BodyVelocity",Bullet)
   		BodyVelocity.Velocity = Char.HumanoidRootPart.CFrame.lookVector * Char.Stats.BulletSpeed.Value 
   		BodyVelocity.maxForce = Vector3.new(math.huge,math.huge,math.huge)
   		
   		game:GetService("Debris"):AddItem(Bullet,3)
   		
   		script.Parent.Color:FireClient(game.Players[Char.Name], Bullet)
   		
   		wait(Char.Stats.ReloadSpeed.Value*.5)
   		local Bullet2 = game.ReplicatedStorage.Bullets.BulletL1:Clone()
   		Bullet2.CFrame = CFrame.new(script.Parent.Tip2.Position)
   		Bullet2.Parent = game.Workspace.SpawnedBalls
   		
   		local BulletDamage = Instance.new("IntValue", Bullet2)
   		BulletDamage.Name = "BulletDamage"
   		BulletDamage.Value = Char.Stats.BulletDamage.Value
   		
   		local CharName = Instance.new("StringValue", Bullet2)
   		CharName.Name = "CharName"
   		CharName.Value = Char.Name
   		
   		local BulletHit = Instance.new("BoolValue", Bullet2)
   		BulletHit.Name = "BulletHit"
   		
   		local BodyVelocity = Instance.new("BodyVelocity",Bullet2)
   		BodyVelocity.Velocity = Char.HumanoidRootPart.CFrame.lookVector * Char.Stats.BulletSpeed.Value 
   		BodyVelocity.maxForce = Vector3.new(math.huge,math.huge,math.huge)
   		
   		game:GetService("Debris"):AddItem(Bullet2,3)
   		
   		script.Parent.Color:FireClient(game.Players[Char.Name], Bullet2)
   		
   		
   		
   		Bullet.Touched:connect(function(Hit)
   			if Hit.Parent ~= Char and Hit.Parent:FindFirstChild("Humanoid") then
   
   				Hit.Parent.Humanoid:TakeDamage(script.Parent.Parent.Stats.BodyDamage.Value)
   				Bullet:Destroy()
   			elseif  Hit.Parent.Name ~= script.Parent.Name and not Hit:IsDescendantOf(Char)and Hit.Parent.Name ~= game.Workspace.SpawnedBalls.Name then
   				--hitsomthing else
   			end 
   		end)
   		game.ReplicatedStorage.RemoteFunctions.Shoot:FireClient(game.Players[Char.Name])
   		Bullet2.Touched:connect(function(Hit)
   			if Hit.Parent ~= Char and Hit.Parent:FindFirstChild("Humanoid") then
   
   				Hit.Parent.Humanoid:TakeDamage(script.Parent.Parent.Stats.BodyDamage.Value)
   				Bullet:Destroy()
   			elseif  Hit.Parent.Name ~= script.Parent.Name and not Hit:IsDescendantOf(Char)and Hit.Parent.Name ~= game.Workspace.SpawnedBalls.Name then
   				--hitsomthing else
   			end 
   		end)
   		game.ReplicatedStorage.RemoteFunctions.Shoot:FireClient(game.Players[Char.Name])
   		wait(Char.Stats.ReloadSpeed.Value*.5)
   			
   		until Firing == false
   	end
   end
end)

script.Parent.Deactivated:Connect(function()
   if Firing == true then
   	Firing = false
   	wait(Char.Stats.ReloadSpeed.Value*.5)
   	CanFire = true
   end
end)

if anyone was wondering how this was fixed i added a larger value to the tick and it was fixed:)