Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to get a specific part of this HttpService GetAsync?

Asked by 4 years ago

What I'm trying to do is to get the amount of visits in a game but whenever I try to do data.visits, it just returns nil but if I just put data by itself, it says everything. How would I exactly print only the visits?

local httpService = game:GetService("HttpService")
local url = "https://games.rprxy.xyz/v1/games?universeIds=2471084"
local res, data = pcall(function()
    return httpService:GetAsync(url, true)
end)
print(res, data.visits)

Output: true nil

0
Answered. yHasteeD 1819 — 4y

1 answer

Log in to vote
0
Answered by
yHasteeD 1819 Moderation Voter
4 years ago
Edited 4 years ago

This returns a Encoded JSON Table, you need to decode it for use. And if you see, in Response have a table called "Data" in that table have another table that you need to open for get Information.

How to decode it? httpService:JSONDecode(Response)

How access this? DecodedJSON.data[1]

Example code:

local HttpService = game:GetService("HttpService")
local Url = "https://games.rprxy.xyz/v1/games?universeIds=2471084"
local Success, Response = pcall(function()
    return HttpService:GetAsync(Url)
end)

if Success then
    local Success, Response = pcall(function()
        return HttpService:JSONDecode(Response)
    end)

    if Success then
        local Information = Response.data[1]
        print("Game Visits:", Information.visits, "Max Players:", Information.maxPlayers)
    else
        print("Can't decode JSON Data")
    end
else
    print("can't get data")
end

Hope it helped! :)

Ad

Answer this question