I know there’s a bunch of debounce modules out there, I just wanted to try making something simple for my first community resource.
debounceutilities
Usage:
--example preventing remote event spam in some server script
local remote = game.ReplicatedStorage.RemoteEvent
remote.OnServerEvent:Connect(function(player)
local allowed = bouncer:check(player.Name)
if not allowed then
return
end
--do something here
end
-
Put module somewhere, I’m putting it in replicatedstorage so both client and server scripts can access it
-
In script:
local db = require(game.ReplicatedStorage.debounceutilities)
- Get bouncer
local bouncer = db.new()
No parameters makes a bouncer that allows 1 check per id, no resetting
optional parameters:
-
Max requests/checks per id (id can be number or string)
-
resetduration (how many seconds until list is reset)
When resetting, all ids (keys) are removed from the bouncer’s list. This prevents memory leaks from new players joining and leaving clogging up the list.
If I want to allow every player to make 5 requests/calls/remote event fires every 30 seconds, I would set up the bouncer like so
local bouncer = db.new(5,30)
Use bouncer:check(id) method to see if that particular id is allowed through. Returns true or false.
if bouncer:check(id) then
--this id hasn't used up all their checks yet
else
--they didn't pass
end
Note: If using a bouncer to guard a remote event listener on a server script, create the bouncer outside the event connection so that all the different firings of the event reference the same bouncer with the same list
local bouncer = db.new(5,30)
...OnServerEvent:Connect(function(player)
if not bouncer:check(player.UserId) then
return
end
--if they got here they passed
end)