Using httpservice to get things from an api


game.Players.PlayerAdded:Connect(function()
	local url = "https://api.hypixel.net/player?key=bd6eb8fe-a454-4602-a888-4b5d0031ad01&uuid=098e5292-2ba0-4772-92dc-6be971c8f3d9"
	local url_sync = http:GetAsync(url)
	local karma = string.find(url_sync,"karma")
	local karma = http:JSONDecode(string.find(url_sync,"karma"))
	local karmaforprint = http:JSONDecode(karma)
	print(karmaforprint)
	print(string.len(karmaforprint))
end)

This is my script. Everything runs fine except for the fact that the numbers being printed is completely off from what’s shown in the website.

1 Like

Try removing that and the karmaforprint variable then print karma

Nope, still 13744 unfortunately

Not sure what to do at all, is there a way i can add the [] without it erroring?

What is the number that should be printed to the output log?

39685 is the number

3Ochrssssssss

still need help, idek what to do

I ran this code in studio, but it only returns 40885:

local http = game:GetService("HttpService")
local url = "https://api.hypixel.net/player?key=bd6eb8fe-a454-4602-a888-4b5d0031ad01&uuid=098e5292-2ba0-4772-92dc-6be971c8f3d9"

local function decode(data: string)
	-- This is used to turn the raw string into a table that Lua can use
	-- The reason why I made this a function is because it makes it easier to type "decode(str)" instead of "http:JSONDecode(str)"
	return http:JSONDecode(data)
end

local function fetch(url: string, callback: (any) -> ()?)
	-- This goes to the provided url and returns the web information (if there's any)
	-- Otherwise, it returns nil
	local success, log = pcall(function()
		return http:GetAsync(url)
	end)
	
	if not success then
		-- If the web request was not successful, this shows a warning in the output log detailing the error information
		warn(log)
	else
		-- This decodes the web information that was recieved and calls the callback function with the information (if one was provided)
		-- Otherwise, it just returns the data that was found
		local data = decode(log)
		
		if callback then
			-- Calls the callback function asynchronously (in separate from the rest of the thread)
			task.spawn(callback, data)
		else
			return data
		end
	end
	
	return nil
end

fetch(url, function(data)
	-- This prints the information we need that was recieved from the web request
	print("karma:",data.player.karma)
end)

Are you sure you’re using the right endpoint?

what’s an endpoint? not sure about that

The endpoint is the url you’re sending the request to, in order to receive information from the webserver. If what’s being displayed on the website is different from what’s being returned, it may have to do with the internal code within the website, it’s the wrong endpoint, or the data being shown on the site is cached information

Yes it’s the correct endpoint.
But I have a question, how do I get to the number? I’m using the find function to get to the variable representing the karma value but idk how to actually get to the karma value itself

The code I created decodes the returned web information into a table that Lua can read from. The karma value is under the player table, but as I stated earlier, that returns 40885, not 39685:

I’ll update the code I sent to include information about how it works, in case you’re confused