local P={}local Y={}function P.Check(Q,P,r,n,S,D)local W=n or DefaultMaxRatePerSec local Z=S or DefaultIdentifier if not Y[P]then Y[P]={}local r local function n(n)if n==P then Y[P]=nil r:Disconnect()end end r=game.Players.PlayerRemoving:Connect(n)end if not Y[P][r]then Y[P][r]={}end if not Y[P][r][Z]then Y[P][r][Z]={}end if W>D then if Y[P][r][Z].Count then local n=tick()-Y[P][r][Z].StartTime if n>=1 then Y[P][r][Z].Count=1 Y[P][r][Z].StartTime=tick()return true else Y[P][r][Z].Count=Y[P][r][Z].Count+1 return Y[P][r][Z].Count<=W end else Y[P][r][Z].Count=1 Y[P][r][Z].StartTime=tick()return true end end if W<=D then if Y[P][r][Z].LastTime then local n=tick()-Y[P][r][Z].LastTime if n>=1/W then Y[P][r][Z].LastTime=tick()return true else return false end else Y[P][r][Z].LastTime=tick()return true end end end return P


local FloodCheck = {}

local Requests = {}

function FloodCheck:Check(Player_Object, Remote, RatePerSec, Unique_Identifier, Seconds)
    local Rate = RatePerSec or DefaultMaxRatePerSec
	local Identifier = Unique_Identifier or DefaultIdentifier
	
	if not Requests[Player_Object] then
		Requests[Player_Object] = {}
		local connection
		local function PlayerLeft(player)
			if player == Player_Object then
				Requests[Player_Object] = nil
				connection:Disconnect()
			end
		end
		connection = game.Players.PlayerRemoving:Connect(PlayerLeft)
	end
	if not Requests[Player_Object][Remote] then
		Requests[Player_Object][Remote] = {}
	end
	if not Requests[Player_Object][Remote][Identifier] then
		Requests[Player_Object][Remote][Identifier] = {}
	end

	if Rate > Seconds then
		if Requests[Player_Object][Remote][Identifier]["Count"] then
			local TimeElapsed = tick() - Requests[Player_Object][Remote][Identifier]["StartTime"] 
			if TimeElapsed >= 1 then
				Requests[Player_Object][Remote][Identifier]["Count"] = 1
				Requests[Player_Object][Remote][Identifier]["StartTime"] = tick()
				return true
			else
				Requests[Player_Object][Remote][Identifier]["Count"] = Requests[Player_Object][Remote][Identifier]["Count"] + 1
				return Requests[Player_Object][Remote][Identifier]["Count"] <= Rate
			end
		else
			Requests[Player_Object][Remote][Identifier]["Count"] = 1
			Requests[Player_Object][Remote][Identifier]["StartTime"] = tick()
			return true
		end
	end
	if Rate <= Seconds then
		if Requests[Player_Object][Remote][Identifier]["LastTime"] then
			local TimeElapsed = tick() - Requests[Player_Object][Remote][Identifier]["LastTime"]
			if TimeElapsed >= (1/Rate) then
				Requests[Player_Object][Remote][Identifier]["LastTime"] = tick()
				return true
			else
				return false
			end
		else
			Requests[Player_Object][Remote][Identifier]["LastTime"] = tick()
			return true
		end
	end
end

return FloodCheck
