Help With Tables/Arrays for Badge List

I have asked on the forum before in regards to making a badge list and someone pointed me in the direction for tables/arrays. After searching through the tables and arrays portion of scripting, I ended up finding a tutorial on here that helped a lot for getting me to understand. The only problem, for me personally, is that I can’t figure out how to add multiple values on the table/array. I can only get the first badge to work properly. For perspective, I am a beginner scripter so I am still learning as I go. Here is the script I’ll insert below.

local badgeService = game:GetService("BadgeService")
local runService = game:GetService("RunService")
local players = game:GetService("Players")

local player = players.LocalPlayer

local badgesHolder = player.PlayerGui:WaitForChild("shop"):WaitForChild("ShopFrame"):WaitForChild("BadgeFrame")

local badges = {
	[1] = {
		["name"] = "Fruit", -- change to the name of the frame that holds the badge's ui
		["displayName"] = "Fruit Ninja", -- change to the name of the badge
		["displayDescription"] = "Guess all the Fruit",
		['id'] = 2125583474, -- change to badge id	

	[2] = {
		["name"] = "ColorsFrame", -- change to the name of the frame that holds the badge's ui
		["displayName"] = "Color Expert", -- change to the name of the badge
		["displayDescription"] = "Guess all the Colors",
		['id'] = 2125585473, -- change to badge id	
			
	[3] = {
		["name"] = "Sport", -- change to the name of the frame that holds the badge's ui
		["displayName"] = "Sport Genius", -- change to the name of the badge
		["displayDescription"] = "Guess all the Sports",
		['id'] = 2125586188, -- change to badge id	

			}
		}
	}
}

local function getUserOwnedBadges()
	for _, data in ipairs(badges) do
		local badgeId = data.id
		local badgeName = data.displayName
		local badgeDescription = data.displayDescription
		local frameName = data.name

		local frameInstance = badgesHolder:FindFirstChild(frameName)
		if not frameInstance then warn(string.format("Could not find badge frame with name of %s", frameName)) continue end
		local badgeNameInstance = frameInstance:WaitForChild("BadgeName")
		local badgeIconInstance = frameInstance:WaitForChild("BadgeIcon")
		local badgeDescriptionInstance = frameInstance:WaitForChild("BadgeDescription")
		local success, ownsBadge = pcall(function()
			return badgeService:UserHasBadgeAsync(player.UserId, badgeId) 
		end)
		if ownsBadge then

			badgeIconInstance.ImageColor3 = Color3.fromRGB(255, 255, 255)
			badgeNameInstance.Text = badgeName
			badgeDescriptionInstance.Text = badgeDescription
		else
			badgeIconInstance.ImageColor3 = Color3.fromRGB(0, 0, 0)
			badgeNameInstance.Text = "???"
			badgeDescriptionInstance.Text = "???"
		end
	end
end

runService.Heartbeat:Connect(getUserOwnedBadges)

The tutorial I followed and the original script is here btw: How to make a badge list

The first badge thing works but not the other two I added. I’m honestly just confused on how to make it work for all of the badges. Any help would be appreciated!

glad my tutorial could help u.

It’s a fairly simple fix:

local badgeService = game:GetService("BadgeService")
local runService = game:GetService("RunService")
local players = game:GetService("Players")

local player = players.LocalPlayer

local badgesHolder = player.PlayerGui:WaitForChild("shop"):WaitForChild("ShopFrame"):WaitForChild("BadgeFrame")

local badges = {
	[1] = {
		["name"] = "Fruit", -- change to the name of the frame that holds the badge's ui
		["displayName"] = "Fruit Ninja", -- change to the name of the badge
		["displayDescription"] = "Guess all the Fruit",
		['id'] = 2125583474, -- change to badge id	
	},
	[2] = {
		["name"] = "ColorsFrame", -- change to the name of the frame that holds the badge's ui
		["displayName"] = "Color Expert", -- change to the name of the badge
		["displayDescription"] = "Guess all the Colors",
		['id'] = 2125585473, -- change to badge id	
	},	
	[3] = {
		["name"] = "Sport", -- change to the name of the frame that holds the badge's ui
		["displayName"] = "Sport Genius", -- change to the name of the badge
		["displayDescription"] = "Guess all the Sports",
		['id'] = 2125586188, -- change to badge id				
	}
}

local function getUserOwnedBadges()
	for _, data in ipairs(badges) do
		local badgeId = data.id
		local badgeName = data.displayName
		local badgeDescription = data.displayDescription
		local frameName = data.name

		local frameInstance = badgesHolder:FindFirstChild(frameName)
		if not frameInstance then warn(string.format("Could not find badge frame with name of %s", frameName)) continue end
		local badgeNameInstance = frameInstance:WaitForChild("BadgeName")
		local badgeIconInstance = frameInstance:WaitForChild("BadgeIcon")
		local badgeDescriptionInstance = frameInstance:WaitForChild("BadgeDescription")
		local success, ownsBadge = pcall(function()
			return badgeService:UserHasBadgeAsync(player.UserId, badgeId) 
		end)
		if ownsBadge then

			badgeIconInstance.ImageColor3 = Color3.fromRGB(255, 255, 255)
			badgeNameInstance.Text = badgeName
			badgeDescriptionInstance.Text = badgeDescription
		else
			badgeIconInstance.ImageColor3 = Color3.fromRGB(0, 0, 0)
			badgeNameInstance.Text = "???"
			badgeDescriptionInstance.Text = "???"
		end
	end
end

runService.Heartbeat:Connect(getUserOwnedBadges)

you were putting your tables directly in each other which was why the first one was the only one working.

@Paige8130

2 Likes

Ah thank you! I figured it was an easy fix like that. I really appreciate the help by the way. The tutorial you posted helped me understand the concept of tables/arrays better than some of the other examples I found!

1 Like

thanks that makes my day!

Glad I could help and happy developing :slight_smile:

1 Like