Script warning says I cant add more than 10 badge ids

I have a script that makes a percentage of how much badges the player has. For example: player has half of the badges then the text would change to 50% if the player has all the badges it would be 100% and if the player has no badges it would be 0% The script works fine but after I add more than 10 badge ids to this script it has this error.

local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer

local badgeIds = {1155034684402098, 1092483073625346, 1443922083423109, 2286001289211241, 2877207132009172, 2803539730865554, 2731922605178227, 1636441079784666, 1543516122206012, 1084222717387909, 1923420383931874
	}

local function calculateBadgePercentage(totalBadges, ownedBadges)
	if totalBadges == 0 then
		return 0
	end
	return math.floor((ownedBadges / totalBadges) * 100)
end

local function updateBadgePercentage()
	local success, result = pcall(function()
		return BadgeService:CheckUserBadgesAsync(player.UserId, badgeIds)
	end)

	if not success then
		warn("Error while checking badges for player", player.Name, ":", result)
		return
	end

	local ownedBadgeIds = result
	local totalBadges = #badgeIds
	local ownedBadges = 0

	for _, badgeId in pairs(badgeIds) do
		if table.find(ownedBadgeIds, badgeId) then
			ownedBadges = ownedBadges + 1
		end
	end

	local percentage = calculateBadgePercentage(totalBadges, ownedBadges)
	script.Parent.Text = percentage .. "%"
end

updateBadgePercentage()


If you can help me with this I would really appreciate it.

2 Likes

because CheckUserBadgesAsync can only check 10 badges at a time, what i would do is just call CheckUserBadgesAsync a second time with the other ids

1 Like

and how do i do that in this script?

Im still waiting on a solution of how to do this

maybe you could make a table for the other badge ids and then call CheckUserBadgesAsync after the first one

I tried that in multiple different ways i just cant get it to work

UserHasBadgeAsync has a higher rate limit than CheckUserBadgesAsync, using this instead should overcome that limit.

local ownedBadgeIds = {}

for _,id in pairs(badgeIds) do
	local success, result = pcall(function()
		return BadgeService:UserHasBadgeAsync(player.UserId, id)
	end)
	if success then
		table.insert(ownedBadgeIds, id)
	else
		warn("Error while checking badges for player", player.Name, ":", id)
		return
	end
end

Where do I put this at in the script? im kinda new to scripting

You’d switch out

local success, result = pcall(function()
	return BadgeService:CheckUserBadgesAsync(player.UserId, badgeIds)
end)

if not success then
	warn("Error while checking badges for player", player.Name, ":", result)
	return
end

local ownedBadgeIds = result

(Just after the start of the funcion updateBadgePercentage)

Full amended code
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer

local badgeIds = {1155034684402098, 1092483073625346, 1443922083423109, 2286001289211241, 2877207132009172, 2803539730865554, 2731922605178227, 1636441079784666, 1543516122206012, 1084222717387909, 1923420383931874
	}

local function calculateBadgePercentage(totalBadges, ownedBadges)
	if totalBadges == 0 then
		return 0
	end
	return math.floor((ownedBadges / totalBadges) * 100)
end

local function updateBadgePercentage()
	local ownedBadgeIds = {}

	for _,id in pairs(badgeIds) do
		local success, result = pcall(function()
			return BadgeService:UserHasBadgeAsync(player.UserId, id)
		end)
		if success then
			table.insert(ownedBadgeIds, id)
		else
			warn("Error while checking badges for player", player.Name, ":", id)
			return
		end
	end
	
	local totalBadges = #badgeIds
	local ownedBadges = 0

	for _, badgeId in pairs(badgeIds) do
		if table.find(ownedBadgeIds, badgeId) then
			ownedBadges = ownedBadges + 1
		end
	end

	local percentage = calculateBadgePercentage(totalBadges, ownedBadges)
	script.Parent.Text = percentage .. "%"
end

updateBadgePercentage()

The text is changing to 100% but I only have a single badge so it should be 9%

My bad, forgot to check if they had it or not.

local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer

local badgeIds = {1155034684402098, 1092483073625346, 1443922083423109, 2286001289211241, 2877207132009172, 2803539730865554, 2731922605178227, 1636441079784666, 1543516122206012, 1084222717387909, 1923420383931874
	}

local function calculateBadgePercentage(totalBadges, ownedBadges)
	if totalBadges == 0 then
		return 0
	end
	return math.floor((ownedBadges / totalBadges) * 100)
end

local function updateBadgePercentage()
	local ownedBadgeIds = {}

	for _,id in pairs(badgeIds) do
		local success, result = pcall(function()
			return BadgeService:UserHasBadgeAsync(player.UserId, id)
		end)
		if success and result then
			table.insert(ownedBadgeIds, id)
		elseif not success then
			warn("Error while checking badges for player", player.Name, ":", id)
			return
		end
	end
	
	local totalBadges = #badgeIds
	local ownedBadges = 0

	for _, badgeId in pairs(badgeIds) do
		if table.find(ownedBadgeIds, badgeId) then
			ownedBadges = ownedBadges + 1
		end
	end

	local percentage = calculateBadgePercentage(totalBadges, ownedBadges)
	script.Parent.Text = percentage .. "%"
end

updateBadgePercentage()

It finally worked ty ive been looking for a solution for a while now

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.