RequestService
Ever wanted to implement cooldowns, the easy way?
Then this module will definitely help you, it is simple yet powerful, and can also be taken as a tutorial.
Why should I use it?
Not only does RequestService have support for different executors (aka players, âROBLOXâ, or even your dog!). But it also handles multiple topics!
How to install:
- Get the most up to date version from the Roblox library
Example of use:
(plr.UserId should be wrapped inside tostring()!)
How does it work?
RequestService has a dictionary that contains each topic you have set, and each topic has a table of executors. As seen in this example below:
local requests = {
["DataStoreCooldowns"] = {
["JohnCena"] = 1234567890,
["TheRock"] = 0987654321,
}
}
Each executor stores the last time it was allowed to proceed from a request in the given topic. Weâll refer to âtableNameâ later as âtopicâ, because itâs easier to understand.
A topic is automatically created if it doesnât exists when you call the function:
requestService:RequestToProceed("CooldownTopicExample")
However, we are not done yet, we cannot call the function yet since we are missing some argumentsâŠ
We also need the desired âcooldownâ, or in nerd explanation: the time that must have passed in seconds since the last successful request. Letâs use as an example, 5.
We also need an executor, which in this example case, would be our player. We will use its userId.
requestService:RequestToProceed("CooldownTopicExample", 5, tostring(plr.UserId))
You can also set âglobalâ cooldowns for every topic by just setting the executor to the same one and just using that executor in particular.
requestService:RequestToProceed("CooldownTopicExample", 5, "ROBLOX")
If the given topic did not exist, it will be created as specified before.
If the given executor did not exist in the given topic, it will be created and be allowed to proceed, because there was no such âlast requestâ in that topic.
Proper usage:
This module can be used server-sided and client-sided, however, it is recommended for important things like datastore cooldowns to be checked in the server.
Keep in mind that requests will not replicate client <-> server!
Other utility:
RequestService also gives you the ability to do things when a request is successful, just do:
requestService:OnRequest(function(requestInformation)
print("A request was successful! Info: ", requestInformation)
end)
When a request is successful, this event will run the function you define here and pass a table containing the information about the request.
If you wish to disconnect the event, just do:
local connection = requestService:OnRequest(function(requestInformation)
print("A request was successful! Info: ", requestInformation)
end)
--
connection:Disconnect()
What if I want to read ALL requests?
Well, thereâs a function exactly made for that:
local allRequests = requestService:GetAllRequests(copy)
But what is copy? Copy is an optional parameter, if set to true, it will return you an identical copy of the requestsTable instead of the referenciation of it.
The parameter is useful when you want to prevent changes in the original requestsTable by your code.
Usage and API:
All documentation is inside the ROBLOX module, and it is Open Source.
If you have any suggestions, please let me know in the comments