--[[
	Anti-Spam
	For Click Detectors

	<Function> Wrap(<Function> FunctionToHook, <Int> OverrideNumberOfAttemptsAllowedBeforeCooldown)

	How to use;
		game:GetService("Workspace").INSTANCE.ClickDetector.MouseClick:Connect(Wrap(function()
			--// Code here
		end), OPTIONALCLICKSPAMPARAMETER)
	
		OR
	
		game:GetService("Workspace").INSTANCE.ClickDetector.MouseClick:Connect(Wrap(FUNCTION), OPTIONALCLICKSPAMPARAMETER)

	Phoenix
]]


local AntiSpam = {} -- Index: <instance> Player | Key: <int> Number of attempts
local DelayQueue = {} -- Index: <instance> Player | Key: <boolean> Delaying
local DefaultSpam = 15

coroutine.wrap(function()
	while true do
		wait(0.25)
		
		for Player, Attempts in pairs(AntiSpam) do
			if Attempts > 1 then
				AntiSpam[Player] = Attempts - 1
			end
		end
	end
end)()

return function(Function, OptionalCooldownParameter) --// Function that prevents spam for global controls eeeeyeeet
	local SpamStop

	if tonumber(OptionalCooldownParameter) ~= nil then
		SpamStop = tonumber(OptionalCooldownParameter)
	else
		SpamStop = DefaultSpam
	end
	
    local Delay = function(Player)
        DelayQueue[Player] = true
        
        wait(5)
        
        DelayQueue[Player] = false
        AntiSpam[Player] = 0
    end

	
    
    return function(Player)
        if AntiSpam[Player] == nil then
            AntiSpam[Player] = 0
        end
        
        if DelayQueue[Player] == nil then
            DelayQueue[Player] = false
        end

        if AntiSpam[Player] < SpamStop then
            AntiSpam[Player] = AntiSpam[Player] + 1
            Function(Player)
        elseif AntiSpam[Player] >= SpamStop and DelayQueue[Player] == false then
            Delay(Player)
        end
    end
end
