Getting a Player's Reputation Through RBX.Lua
Posted on August 28, 2015 by AmericanStripes
This piece was written by HungryJaffer. Go show him some love for the amazing article.
Now here's how it all started.
About 2 weeks ago, I was browsing scriptinghelpers, and came across this answer to this question. Amazed, I wanted to explore more possibilities of :GetAsync()
. But what is GetAsync, you ask?
According to the wiki, :GetAsync()
, of HttpService
Sends a HTTP GET request to the supplied URL and returns the response body in a string.
So how do we use this to check a player's reputation?
Well, first, we want to get the person who will be checking, I'll use NotSoNorm as an example.
local nom = "NotSoNorm" -- Create a string variable of the name local link = "https://scriptinghelpers.org/user/".. nom -- We will Concatenate the link and the name.
Now that we have that done, we're going to get into :GetAsync()
. The first thing we want to do with GetAsync is get the HTML form of the website ScriptingHelpers.
The first thing we want to do when using GetAsync()
is to enable HttpEnabled. This is a property of HttpService.
Then, we want to identify HttpService using :GetService()
local Service = game:GetService("HttpService")
Now that we have HttpService, we want to send a HTTP GET to the Scripting Helpers website.
local Service = game:GetService("HttpService") local html = Service:GetAsync(link, false)
If I decided to print html, it would print the whole html representation of the web page. It would be too much to put on here.
Now that we have the HTML representation of Scripting Helpers (Specifically the profile view), we want to find the snippet of HTML that pertains to the Reputation. This can be easily done by right clicking the reputation under a player's profile, and clicking on Inspect Element.
Right click on reputation then go down to Inspect Element. This is what we will get. This line of HTML is very important.Now that we have the line of HTML that we need, we can use our knowledge of string manipulation find this snippet of code in anyone else's username.
We will use string.match
to find this specific line in the whole jumble of HTML we got through :GetAsync()
local string = [[<div>%d+ reputation</div>]] local snippet = html:match(string)
We're almost done here. Now that we have the snippet we need, all we have to do is use string.match, and the string pattern %d+
(this represents a number of any length) to get the number of rep that the said player has.
local string = [[<div>%d+ reputation</div>]] local snippet = html:match(string) Reputation = (snippet:match("%d+"))
And turning this into a function and getting our end product, this is what we get.
function getRep(Player) local nom = Player -- Create a string variable of the name local link = "https://scriptinghelpers.org/user/".. nom -- We will Concatenate the link and the name. local Service = game:GetService("HttpService") local html = Service:GetAsync(link, false) local string = [[<div>%d+ reputation</div>]] local snippet = html:match(string) Reputation = (snippet:match("%d+")) return ("Player has " .. Reputation .. " reputation on Scripting Helpers") end
And voila, a function that returns the amount of rep any player on SH has.
Commentary
Leave a Comment