I am messing with the leaky bucket algorithm, but am not sure where I am going wrong with my code.
I am following this thread from the devforum if you need better examples of what is going on here.
For my scenario my gun fires a max of 8 times per second, and in the case the of remote lag, I can't just use a debounce for 1/8th of a second. I am trying to fix up my code to make sure that if for some reason this happened it wouldn't cause problems.
Second 1: 4 bullets fire
Second 2: 12 bullets fire
With an average of 8 shots over 2 seconds, no disturbance for a normal player, but blocks the chance of an exploiter sending >8 average per second.
Link to the devforum thread: https://devforum.roblox.com/t/how-to-check-if-a-player-is-spamming-a-remote-too-fast/245991/14
My current code
local var = game.ReplicatedStorage:WaitForChild("Rem") local counter = {} local threshold = 8 var.OnServerEvent:Connect(function(player) if not counter[player] then counter[player] = 1 else counter[player] = counter[player] + 1 if counter[player] > threshold then warn("Flood detected!") else print("Tick", counter[player]) -- Do my gun stuff here end end end) local function yes() while true do wait(1) for i,v in pairs(counter) do if v > 0 then counter[i] = math.max(0, v - 8) end end end end spawn(yes)
I tried changing this line if counter[player] > threshold then
to if counter[player] > threshold + 8 then
which is what the original thread said to do.
This issue here is an exploiter could continuously fire the remote 16 times without issue if they wanted.
This was fine I was testing in an odd way
At the end of your function connecting with the signal (OnServerEvent), I would spawn a function on another thread that waits a couple of seconds before setting the player's counter back to 1. The time you wait is your choice.
Also your script will make it so their first shot won't fire due to the if statement having an else, I would remove the else.