Effective way to handle bullet fire rate

I want to be able to prevent clients from firing more bullets than they are allocated, based on what gun they have equipped. However, I am struggeling to find the most efficient way to do so, considering I’d need 3 different ways of managing this, as there’s I guess 3 different gun fire rates.

All I’ve got at the moment is the basic guns, which are 1 bullet per click. However, I need a way to handle the Auto weapons (can fire multiple bullets per second), as well as guns with a “Spread” value (these guns are like shotguns, and thus will fire 6 bullets at once)

if AUTO then -- Auto weapon, need to stockpile how many bullets they can shoot a second
	
else
	local CurrentTick = tick()
	local FirstShot = false
	
	if SPREAD > 0 then -- Guns with spread fire 6 shots at once, allocate 6 shots per FIRE_RATE

	else			
		if not PlayersFired[player.UserId] then -- Haven't fired a bullet yet
			PlayersFired[player.UserId] = CurrentTick
			
			FirstShot = true
		end
		
		if not FirstShot then	
			local WaitTime = FIRE_RATE
			
			if PlayersFired[player.UserId] + WaitTime > CurrentTick + THRESHOLD then
				warn(player, "tried firing before cooldown!")
				
				return
			end
			
			PlayersFired[player.UserId] = CurrentTick
		end
	end
end
1 Like

I would create a function that fires one bullet with a given speed and a direction.

This function gets fired different times and with different directions depending on the gun.

This one function can be used by all the guns but you’ll still need to control it per gun.