How would I make a debounce but every time it fired 5 times

The name has poorly explained it, apologies.

Okay so, yes I do know how to make debounces, however this is tricky. What if I wanna make a debounce like 30 requests per minute? I tried everything and no luck, here’s my current code

local cooldown = os.time()

local function sendrequest(url,data)
if os.time() - cooldown > 60 then
httpservice:PostAsync(url,data)
cooldown = os.time()
end
end

I need this for my discord webhook. Pls help

1 Like

Make a variable counter for and every request add 1 to the variable. Then put a if statement if the request already filled up(has 30 request) then debounce it for 1 min. Then use a remotefunction to return to alarm the user that the queue already filled up so they can’t run his request yet.

1 Like

That’s what I was thinking. The problem is I don’t know how to check them both.

1 Like

What do you mean by both? Is it for client for all of the user? Is it for all of the servers in game?

1 Like

No I meant I don’t know how to check cooldown and the maxrequests at the same time

1 Like

According to this thread, the max limit is 500. And i assume there will be alot of servers in the future on your game, so i recommend you using pcall if the request was accepted.

This is completely unrelated and I already use it, and the discord ratelimits is more limited than httpservice limits, so it shouldn’t matter much

@regexman

local debounce = true

local requests = 0

local function sendrequest(url,data)
if requests < 30 and debounce == true then

requests += 1

httpservice:PostAsync(url,data)
task.wait(1)
debounce = true
end
end
while task.wait(60) do
requests = 0
end

If u have any questions then feel free to ask

Here’s a more reliable/efficient version which doesn’t rely on an indefinite loop which yields the entire script.

local Http = game:GetService("HttpService")

local Debounce = false
local Requests = 0
local ProtectedCall = pcall
local Tick = tick
local Time = Tick()

local function SubmitRequest(URL, Data)
	if Debounce then
		return
	end
	Debounce = true
	task.delay(0.1, function()
		Debounce = false
	end)
	
	if tick() - Time > 60 then
		Time = Tick()
		Requests = 0
	end
	
	Requests += 1
	
	if Requests > 30 then
		return
	end
	
	local Success, Result = ProtectedCall(function()
		return Http:PostAsync(URL, Data)
	end)
	
	if Success then
		print(Result)
		--Do something with the response of the http request here.
		return Result
	else
		warn(Result)
	end
end

y’know you could’ve just done

local debounce = true

local requests = 0

local function sendrequest(url,data)
if requests < 30 and debounce == true then

requests += 1

httpservice:PostAsync(url,data)
task.wait(1)
debounce = true
end
end
task.spawn(function()

while task.wait(60) do
requests = 0
end
end)

You should avoid creating additional threads of execution when it isn’t necessary to do so.

You should also be wrapping your http requests inside pcall() otherwise if the request fails the script will error causing it to break.