HttpService for Dummies
Difficulty Level: 4/5 Stars
Hey everyone, I'm back with another tutorial. HttpService is a concept that's easy to grasp but takes practice. I put the difficulty level at 4/5 stars because it depends on what API -- Application Programming Interface -- you use. For example there's a Google API Library, Oxford Dictionary API, and the list goes on. These can all be used inside of roblox.
In this tutorial we'll be using a News API. Once at the News API link you should click on View All and then press Google News, or click here to access it. Now, press Get API Key. It will make you sign up, and go through a process. Once you're done going through the process it should give you a bunch of random letters and numbers. This is your API Key. Write it down so you don't forget it.
Now, this is an extremely important link you're going to use:
Go into roblox studio and create a server sided script inside of ServerScriptService. Open up the script and type:
local http = game:GetService("HttpService") local url = "https://newsapi.org/v1/articles?source=google-news&sortBy=top&apiKey=API_KEY" local data = http:GetAsync(url, false) -- Sends out a request to the url and then returns some information. This information is a giant table in JSON. local decodedData = http:JSONDecode(data) -- Uses the method JSONDecode in order to turn the data from JSON format into lua format. print(decodedData.articles.title) -- That will print the title of the first article.
Replace API_KEY in the url with your API Key.
Press Play and in your output you should see the title of an article.
You can also do:
print(decodedData.articles.description) -- Prints the article description.
print(decodedData.articles.author) -- Prints the author of the article.
You can advance this with basic lua like for i,v in pairs() loops and use GUIs. I ended up creating a GUI newspaper:
As you can see, this works very well. You can use different APIs and follow their online tutorials to do this with dictionaries, translators, and even voice modules!
Hope I explained this well, and be sure to give me some feedback!