How to get the JSONDecode from the API

Hello developers,

I’m having some issues with the Roblox API (it’s my first time using it) and I can’t get the JSONDecode from the Official Roblox Badges.

local HttpService = game:GetService("HttpService") 
game.Players.PlayerAdded:Connect(function(player)
	print(player.Name, player.UserId)
	local id2 = player.UserId
	local IsRBXAdmin = false
	local PlayerBadges = HttpService:GetAsync("https://accountinformation.roproxy.com/v1/users/"..id2.."/roblox-badges")
	print(PlayerBadges)
	local data = HttpService:JSONDecode(PlayerBadges)
	print(data.id)
        print(data.name)
        print(data.description)
end)

What I want to achieve is to put these results in a GUI, but I can’t get the JSONDecode to be able to put them in their corresponding order

This if it works

[ print(PlayerBadges)]

but … this doesn’t work if it returns null (nil)

    print(data.id)
    print(data.name)
    print(data.description)

If you have any solutions, please let me know. :pray:

It returns an array of badges, so “data” is an array that you need to iterate

It looks something like this

{
    {
        id = 1,
        name = "name1"
    },
    {
        id = 2,
        name = "name2"
    }
}

so you do something like

for i, badgeData in pairs(data) do
    print(badgeData.id, badgeData.name, badgeData.description)
end
1 Like

Oh, Thanks!!! :heart_eyes:

30 crater

Another question, to be able to put a badge in each text box, how would you go about it?

I have tried to do this but it gives me an error.

	if badgeData.name == "Veteran" then
			player.PlayerGui:FindFirstChild("ScreenGui").Frame.TextBox.Text = badgeData.name
		end

I would send the data from the server to the client thru a remote event and then let the client handle the text box editing.

e.g.

-- server
if badgeData.name == "Veteran" then
    remote:FireClient(player, "Veteran")
end

-- client
remote.OnClientEvent:Connect(function(badgeName)
    TextBox.Text = badgeName
end)

What’s the error?

1 Like

ServerScriptService.Script:12: attempt to index nil with ‘Frame’

I’ll try

That means there is no screen gui at the time the server script ran, so maybe you can use :WaitForChild() instead of :FindFirstChild()

Ready, it worked!

Thanks again! :sweat_smile:

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

Players.PlayerAdded:Connect(function(player)
	local playerGui = player:WaitForChild("PlayerGui")
	local screenGui = playerGui:WaitForChild("ScreenGui")
	local frame = screenGui:WaitForChild("Frame")
	local textbox = frame:WaitForChild("TextBox")
	print(player.Name, player.UserId)
	local id2 = player.UserId
	local IsRBXAdmin = false
	local PlayerBadges = HttpService:GetAsync("https://accountinformation.roproxy.com/v1/users/"..id2.."/roblox-badges")
	print(PlayerBadges)
	local data = HttpService:JSONDecode(PlayerBadges)
	for _, badge in ipairs(data) do
		if badge.name == "Veteran" then
			textbox.Text = badge.name
		end
	end
end)

I’d do it like this to ensure the entire UI path has loaded before attempting to reference it further from within the same script.

1 Like