Cooldown Sanity Check For Guns

My current setup is basically having a firerate value and checking if the time that has elapsed since the last shot is greater than the firerate (the cooldown basically). Same goes for automatic guns but I utilized other variables here to account for the framerate changes.

function startFiring()
	-- also accounts for the ammo
	local coolDown = 60 / Config.Firerate
	if Config.Auto then
		
		-- FireServer when firing begins
		
		local timeElapsed = 0
		local frameDelta = 0
	
		repeat
			timeElapsed = timeElapsed + frameDelta
			if timeElapsed >= coolDown then
				
				timeElapsed = timeElapsed - coolDown
			end
			frameDelta = RunService.Heartbeat:Wait()

		until firing == false

		-- FireServer when firing ends
	else -- if not automatic
		local timeElapsed = os.clock() - fireDebounce
		if timeElapsed >= coolDown then

			FireRequest:FireServer(firearm) -- does the sanity check and enables a canFire player debounce
			
			fireDebounce = os.clock()
		end
	end
end

The issue is I’m planning on doing a sanity check for when the gun has been fired to see if the player is not getting the gun spammed way above the firerate. How will I check for the time elapsed on the server side as well so I can compare it to that of the client? Should I also do the same thing as I did on the client (same setup) but on the serverside? Anything I’m doing that is wrong? First time working with sanity checks of this scale.

  1. Track the last action
  2. Calculate the elapsed time, when the player tries to preform the action again just have a check to compare it to the cooldown
  3. Repeat

make a variable for the last time the gun was fired. Compare the current time and the previous time, if the current time - previous time is accurate to the firerate then the previous time becomes the current time and the bullet gets fired

Now that I think about it, I could just record the time from when I started the :FireServer to the next :FireServer and compare that with the one I did on the client.

1 Like