Hi, I’m making free badges game, but I have an question, if I have this script:
game.Players.PlayerAdded:Connect(function(Player)
while wait(3) do
local Count = 0
local http = game:GetService("HttpService")
local PlaceId = game.PlaceId
local data = http:GetAsync("https://apis.roproxy.com/universes/v1/places/"..PlaceId.."/universe")
if data then
data = http:JSONDecode(data)
local universeId = data.universeId
data = http:GetAsync(`https://badges.roproxy.com/v1/universes/{universeId}/badges?limit=100&sortOrder=Asc`)
if data then
data = http:JSONDecode(data)
for _, Badge in data.data do
if not game:GetService("BadgeService"):UserHasBadgeAsync(Player.UserId, Badge.id) then
game:GetService("BadgeService"):AwardBadge(Player.UserId, Badge.id)
print("Successfully awarded badge with name '"..Badge.name.."' and Id '"..Badge.id.."' to player called "..Player.Name..".")
end
for _, Badge in data.data do
Count = #data.data
Player.PlayerGui:WaitForChild("Badges"):WaitForChild("TotalCount").Text = "Total available (earnable) badges count: "..Count.."."
end
end
end
end
end
end)
How can I make an TextLabel, that will write correct active badges count? I’ve tried too many methods, but all of them doesn’t working. So, I wan’t to make a label that will show all only active badges in the game (it should to don’t count the inactive ones). Help!
local function UpdateBadgeCount(player)
local activeBadgeCount = 0
local http = game:GetService("HttpService")
local PlaceId = game.PlaceId
local data = http:GetAsync("https://apis.roproxy.com/universes/v1/places/"..PlaceId.."/universe")
if data then
data = http:JSONDecode(data)
local universeId = data.universeId
data = http:GetAsync("https://badges.roproxy.com/v1/universes/"..universeId.."/badges?limit=100&sortOrder=Asc")
if data then
data = http:JSONDecode(data)
for _, Badge in pairs(data.data) do
if not game:GetService("BadgeService"):UserHasBadgeAsync(player.UserId, Badge.id) then
game:GetService("BadgeService"):AwardBadge(player.UserId, Badge.id)
print("Successfully awarded badge with name '"..Badge.name.."' and Id '"..Badge.id.."' to player called "..player.Name..".")
else
activeBadgeCount += 1
end
end
end
end
return activeBadgeCount
end
game.Players.PlayerAdded:Connect(function(player)
local activeBadges = UpdateBadgeCount(player)
-- change ActiveBadgeCount to your textlabel name
local gui = player:WaitForChild("PlayerGui")
local badgeCountLabel = gui:WaitForChild("Badges"):WaitForChild("ActiveBadgeCount")
badgeCountLabel.Text = "Active badges count: "..activeBadges
end)
To show all active badges you just need to make a GET request for game badges and check if given badge has it’s enabled property set to true. Also if game has > 100 badges then you need to make additional requests to fetch all badges.
Example:
local httpService = game:GetService("HttpService")
local function fetchBadges(cursor)
local success, response = pcall(function()
return httpService:GetAsync(`https://badges.roproxy.com/v1/universes/{game.GameId}/badges?limit=100&sortOrder=Asc{if cursor then `&cursor={cursor}` else ``}`) -- UniverseId can be received from game.GameId property
end)
if not success then
warn(response) --Unable to get badges
return {}
end
local success, decoded = pcall(function()
return httpService:JSONDecode(response)
end)
return success and decoded or {}
end
local function getActiveBadges()
local activeCount = 0
local badges = fetchBadges()
while true do
if not badges.data then break end
for _, badge in badges.data do
if badge.enabled then activeCount += 1 end
end
if not badges.nextPageCursor then break end
badges = fetchBadges(badges.nextPageCursor)
end
return activeCount
end
This getActiveBadges() function will return a number of active badges which you can use in a text label. Keep in mind that if you gonna have over 100 badges then counting them can take a while due to ratelimits.