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()
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
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()
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()