How would I add a debounce for this modular framework?

Heya Everyone!!

I’m working on a simple modular framework inspired by EgoMoose’s FPS framework. I’m wondering how I would implement a debounce inside of the framework. How the framework works is that whenever a player equips an item, it’ll cause the module to get the settings module inside of the item which, of course, contains all the information of the gun including Firerate. (Which is self explanatory)

I’m wondering how I would be able to include Firerate to it.

--[[SERVICES]]--
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--[[FOLDERS]]--
local RemoteEvents = ReplicatedStorage:WaitForChild("RemoteEvents")

--[[MODULE]]--
local BaseGunModule = {}
BaseGunModule.__index = BaseGunModule

function BaseGunModule:Equip(Weapon)
	self.Settings = require(Weapon:WaitForChild("Settings"))
end

function BaseGunModule:Fire(Wielder, A, B)
	--//Firerate/Sanity Checking?
	--//?
	
	--//Checking to see if the gun has enough ammo
	if self.Settings.AmmoHold >= 1 then
		--//Firing the gun
		self.Settings.AmmoHold -= 1
		print("Remaining ammo: "..self.Settings.AmmoHold)

		local RaycastParam = RaycastParams.new()
		RaycastParam.FilterDescendantsInstances = {Wielder}
		RaycastParam.FilterType = Enum.RaycastFilterType.Exclude

		local Raycast = workspace:Raycast(A.Position, (B - A.Position).Unit * 5000, RaycastParam)
		local RaycastInstance = Raycast.Instance
		if RaycastInstance then
			local Humanoid = RaycastInstance.Parent:FindFirstChildWhichIsA("Humanoid")
			if not Humanoid then
				--//The raycast didn't hit a humanoid.
				return
			end

			if Humanoid and Humanoid.Health >= 1 then
				Humanoid:TakeDamage(self.Settings.BaseDamage)
				print(RaycastInstance.Parent.Name.." has taken "..self.Settings.BaseDamage.." damage.")
				RemoteEvents.Fire:FireServer(Humanoid, self.Settings.BaseDamage)
			end
		end
	else
		print("Does this even work")
		self:Reload()
	end
end

function BaseGunModule:Reload()
	print("Oh it does, cool.")
	--//Reloading the gun if we have enough ammo.
	if self.Settings.AmmoContain >= 1 then
		self.Settings.AmmoHold += 6
		self.Settings.AmmoContain -= 6
	else
		warn("Find a refill station. You do not have any ammo remaining.")
	end

	print("Remaining ammo: "..self.Settings.AmmoContain)
end

return BaseGunModule

Where self is declared (i.e. where setmetatable occurs), add the variable self.LastFire = 0. At the start of the fire function, do something like:

local now = os.clock() -- or tick()
if now - self.LastFire < self.Settings.FireDebounce then return end
self.LastFire = now

First we take the current time through os.clock(), very accurate and starts when the client loads in. Then we substract this time with the time when the last shot took place, and if the difference is smaller than the debounce time, we stop the function.
If it is greater, we update the last fired time and the fire function continues as usual.

I recommend this approach over a self.Debounce = false as that requires yielding (at the end of the function), and unless you use task.defer your code will look very messy.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.