Hello, could someone give me a hand
I need to get the first pokemon, I’m not sure what I’m doing wrong, could someone give me a hand?
local HttpService = game:GetService("HttpService")
local url = 'https://pokeapi.co/api/v2/pokemon/1'
function printPokemon()
local response
local data
pcall(function()
response = HttpService:GetAsync(url)
data = HttpService:JSONDecode(response)
end)
if not data then
return false
end
if data.message == 'success' and data.pokemon then
print('The pokemon is: ' .. data.poke1)
return true
else
return false
end
end
if printPokemon() then
print("Success")
else
print("Something went wrong")
end
It isn’t working because when making a get request with :GetAsync, it doesn’t return a table with information regarding if it was successful or not, you have to use the success and result arguments returned from pcall:
local httpService = game:GetService('HttpService')
local url = 'https://pokeapi.co/api/v2/pokemon/1'
local success, result = pcall(httpService.GetAsync, httpService, url)
if not success then
return warn(result)
end
local decoded = httpService:JSONDecode(result)
print(decoded)