Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Error in extracting JSON strings?

Basically I have a ROBLOX+ API (an API that is user-created that extracts data from the ROBLOX site) and I'm trying to get the game to return the RAP ( Roblox Average Price) from a user using a User ID. I'm using ROBLOX's User ID which is 1 as an example of how I might do so.

Here is the code I have based on the Blog Post on how to get rep (reputation) from someone on ScriptingHelpers.

function getRap(Player)
    local ID = 1 
    local link  = "http://roblox.plus:2052/inventory?id=".. ID 
    local Service = game:GetService("HttpService")
    local html = Service:GetAsync(link, false)
    local string = [[<div>"rap":%d+</div>]]
    local snippet = html:match(string)
    RAP = (snippet:match("%d+"))

    return ("ROBLOX has " .. RAP .. " ")
end

I was almost sure this would work, but the only thing the output returned was an error I never seen before.

HTTP 403 (HTTP/1.1 403 Forbidden): - path: user/multi-following-exists, 
json: {"otherUserIds":[38096393],"userId":38096393}
ServerSocialScript Loaded

I can't make sense of it. Is there a fix to this?

2 comments

0

That isn't an error, that's just a weird thing studio says now when it launches a test.

0

It only says that in the output when I run this script.

1 answer

✓ Accepted answer

Decode it into a Lua table with HttpService's JSONDecode. From there, you can easily index the RAP.

 local HttpService = game:GetService("HttpService")
 local Players = game:GetService("Players")

function getRapFromUserId(userid) -- I would suggest passing the player's ID rather than their player object
    local link  = "http://roblox.plus:2052/inventory?id=".. userid 
    local json = HttpService:GetAsync(link, false) -- get the json string
    local decoded = HttpService:JSONDecode(json) -- decode the json into a lua table we can index
    local rap = decoded.rap -- index rap from the decoded json
    return rap -- just return the rap
end

print(Players:GetNameFromUserIdAsync(1) .. " has a RAP of " .. getRapFromUserId(1) )

EDIT: If you really want to use string:match(), you made a mistake with your pattern matching. [[<div>"rap":%d+</div>]] should be [["rap":%d+]] instead since JSON doesn't even use <tag>s.

5 comments

1

Be wary that you may have to wait several seconds until the request completes, so be patient. Also make sure that HttpRequests are enabled (You can check this in the properties of HttpService, it will error otherwise anyway)

0

There is an error on line 6 and line 12. Also if you visited the URL then you will probably see that the RAP is near the top and there is a big mess of code everywhere else.

1

It works. I've used this API before. It's just that the site is down as of now (http://roblox.plus:2052/inventory?id=1 will not work). If you go to that link right now in your browser, it will say "Error 522: Connection timed out" It might be fixed several hours from now.

0

I don't see any part of the script separating the RAP from the rest of the output.

1

local rap = decoded.rap

This discussion is preserved as it was. New answers, comments, and votes are closed.