Overview
Hey Developers!
Today I’m going to show you the basics of http requests. By the end of this tutorial, you’re guaranteed to have a functioning system that interacts with external APIS, taking your script further.
Basics
Lets first create a script! Its recommended that this is a serverscript, not local. Once you’ve done that, we need to get httpservice! You can do this by entering the following into your script:
local HttpService = game:GetService("HttpService")
Once you’ve done that, it should look something like this:
Now, We need it to actually do something, right? Now that we’ve got the service, we can now add our external API URL, I’m going to be making a quote system, so we are going to need a API to request from. If you want to use the roblox API, check out the full list + documentation by clicking the arrow below this text.
Collected List Of Roblox APIS
YOU CANNOT USE THE ROBLOX APIS DIRECTLY. PLEASE REFER TO OTHER SERVICES SUCH AS ROPROXY
Anyways, we need to actually get the url from somewhere, right?
This is the service im going to use, read a full documentation of quotable via github.
Please add the following to your script: local URL = "URLHERE"
This is mine for reference:
Explanation:
GET /quotes/random
Get one or more random quotes from the database. This method supports several filters that can be used to get random quotes with specific properties (ie tags, quote length, etc.)
As said before, a full explanation is available via github.
Now that you’ve most likely got your API URL, we can finally get to scripting the system. To make this simple, I’ll be doing step by step tutorials on more common APIS.
Understanding Discord Webhooks In Roblox
Lets start making a webhook system for your game!
!! Discord blocked webhooks that are requested directly from roblox, a proxy is required. Read this before continuing.
Recommended Proxy(s)
https://webhook.lewisakura.moe/
ANYWAYS, lets start making the script. (if you don’t know how to make a webhook, I recommend reading this for more info
- In your Roblox game, create a new script.
- Use Roblox’s
HttpService
to make HTTP requests to the Discord webhook URL. Here’s an example of how to send a message:
local HttpService = game:GetService("HttpService")
local webhookUrl = "URL"
local message = {
content = "Hello World!",
}
local headers = {
["Content-Type"] = "application/json",
}
local success, response = pcall(function()
return HttpService:RequestAsync({
Url = webhookUrl,
Method = "POST",
Headers = headers,
Body = HttpService:JSONEncode(message),
})
end)
if success then
print("Message sent to Discord!")
else
print("Failed to send message:", response)
end
Useful Information:
Triggering the Roblox Script:
- You need to determine when you want to trigger the Roblox script to send a message to Discord. This can be based on in-game events, player actions, or any other condition that fits your game’s logic.
Testing:
- To test the system, run your Roblox game and trigger the script. It should send a message to the specified Discord channel.
Security Considerations:
- Ensure that your webhook URL is kept secure. Do not share it publicly.
Error Handling:
- Implement proper error handling in your Roblox script to deal with cases where the HTTP request to Discord fails.
Rate Limiting:
- Be aware of Discord’s rate limiting policies when sending messages via webhooks. If you send messages too frequently, you may be rate-limited.
By following these steps, you can set up a Discord webhook system that allows your Roblox game to send messages or data to a Discord channel using a webhook.
Using External APIS
Hello! This is a very easy step by step tutorial to help you create a quote system in roblox! Once you understand this you should be able to freely create awesome scripts without using api tutorials!
Part 1: Importing HttpService and Defining the API URL
local HttpService = game:GetService("HttpService")
local URL = "https://api.quotable.io/quotes/random"
- We import the
HttpService
module, which allows us to make HTTP requests. - We define the
URL
variable, which stores the API URL for fetching random quotes.
Part 2: Initializing Variables
local LastSet = ""
- We initialize a variable named
LastSet
to an empty string. This variable will store the last fetched quote.
Part 3: Defining the RequestRandomQuote
Function
function RequestRandomQuote()
- We start defining a function named
RequestRandomQuote
to fetch random quotes from the API.
Part 4: Initializing Local Variables
local content
local author
- Inside the
RequestRandomQuote
function, we initialize two local variables,content
andauthor
, which will store the quote content and author.
Part 5: Using pcall for Error Handling
local success, err = pcall(function()
- We use
pcall
to handle potential errors during the HTTP request. Thesuccess
variable will betrue
if the operation succeeds, anderr
will contain any error message if there’s an issue.
Part 6: Making an HTTP GET Request
local response = HttpService:GetAsync(URL)
- Inside the
pcall
function, we make an HTTP GET request to the API URL usingHttpService:GetAsync()
and store the response in theresponse
variable.
Part 7: Parsing the JSON Response
local responseData = HttpService:JSONDecode(response)
- We parse the JSON response from the API into a Lua table named
responseData
usingHttpService:JSONDecode()
.
Part 8: Extracting Quote Content and Author
content = responseData[1].content
author = responseData[1].author
- We extract the
content
andauthor
values from the response data and store them in the respective variables.
Part 9: Handling Errors
end)
if err then
print(script.Name.." Failed HTTP, ERROR: "..err)
else
- We check if there was an error during the HTTP request. If there’s an error, we print an error message.
Part 10: Creating Formatted Quote Text
local quoteText = content.." - "..author.." "
- We create a formatted
quoteText
by combining thecontent
andauthor
with a hyphen and a space in between.
Part 11: Handling Undefined Content
if content == "undefined" then
quoteText = LastSet or "ERROR"
end
- If the
content
is “undefined,” we setquoteText
to the last fetched quote (LastSet
) or “ERROR” if there’s no previous quote.
Part 12: Printing the Quote and Updating LastSet
print(quoteText)
LastSet = quoteText
end
end
- We print the
quoteText
to the output console. - We update the
LastSet
variable with the new quote text.
Part 13: Continuously Fetching Quotes
while true do
RequestRandomQuote()
wait(20)
end
- We create a loop that continuously calls the
RequestRandomQuote
function to fetch and print random quotes every 20 seconds.
Final Script:
local HttpService = game:GetService("HttpService")
local URL = "https://api.quotable.io/quotes/random"
local LastSet = ""
function RequestRandomQuote()
local content
local author
local success, err = pcall(function()
local response = HttpService:GetAsync(URL)
local responseData = HttpService:JSONDecode(response)
content = responseData[1].content
author = responseData[1].author
end)
if err then
print(script.Name.." Failed HTTP, ERROR: "..err)
else
local quoteText = content.." - "..author.." "
if content == "undefined" then
quoteText = LastSet or "ERROR"
end
print(quoteText)
LastSet = quoteText
end
end
while true do
RequestRandomQuote()
wait(20)
end
They’ll be a more detailed guide soon, these posts just take a very long time to write.
Anyways, this script will get a quote from the api url, and return it back to roblox then printing it into the output.
I apologise if this guide confused anyone. Its currently still being written, I just didn’t wanna lose my mind since I spent around 4-5 hours writing this. If you have any questions I’m completely open. And my replies will most likely be more detailed and answer your concern easily.
But thanks for reading! Hope this helps!