Spam-protecting the client-server boundary

So what I’ve got is a simple server-script that filters a selection on remote events based on an interval cooldown, if a player’s new call has been received whilst the player is still in cooldown, the player would receive a warn, 3 warns and the player is kicked.

local from = game.ReplicatedStorage._events
local to = game.ServerStorage._events
local t = os.clock
local intervaltime = .5

function LoadIn()
	local cache = {}
	local reg = {}
	game.Players.PlayerRemoving:Connect(function(plr)
		if cache[plr] then cache[plr] = nil end
		if reg[plr] then reg[plr] = nil end
	end)
	for _, _e : RemoteEvent in pairs(from:GetChildren()) do
		local counterpart : BindableEvent = to:WaitForChild(_e.Name,2)
		if counterpart then
			_e.OnServerEvent:Connect(function(plr,...)
				if not cache[plr] then cache[plr] = 0 end
				if not reg[plr] then reg[plr] = 0 end
				if cache[plr] + intervaltime > t() then
					reg[plr] += 1
					if reg[plr] > 2 then plr:Kick() end
				else
					cache[plr] = t(); counterpart:Fire(...)
				end
			end)
		end
	end
end

SETUP:

All rEvents effected must have a bEvent counterpart with the same name
image
image
Server scripts must be connected to the bEvents. This also means that requests from other scripts can be sent through the same channel as the requests from clients go through.

If you have any suggestions that improve on my current system, please share them!

2 Likes