local players = game:GetService("Players")
local badges = game:GetService("BadgeService")
local userHasBadge = badges.UserHasBadgeAsync
local badgeIds = {1, 2, 3} --Replace these numbers with the IDs of the game's badges.
local function onPlayerAdded(player)
local ls = Instance.new("Folder")
ls.Name = "leaderstats"
ls.Parent = player
local _badges = Instance.new("IntValue")
_badges.Name = "Badges"
_badges.Parent = ls
for _, badgeId in ipairs(badgeIds) do
task.wait(0.1)
local success, result = pcall(userHasBadge, badges, player.UserId, badgeId)
if success then
if result then
_badges.Value += 1
end
else
warn(result)
end
end
end
players.PlayerAdded:Connect(onPlayerAdded)
Whenever you award badges you’ll need to increment the stat by 1 as well.
Thank you so much! I was wondering how do I make it so that if you earn a badge in-game the leaderboard updates while you’re in-game right away? For instance, I had 8 badges and the leaderboard says 8, I just got a badge and now it should turn 9 right away instead of having to leave and rejoin the server for it to show that I own 9, how do I make that work?
How would I make the value only go up only once? I was thinking of using boolean values to check, but since it’d be in a serverscript I believe it would stop everybody from being able to retrieve it once the value has been changed? How would I do this locally and efficiently
Use a table that tracks each badge that has been earned per player. Whenever a player collects a badge check to see if that badge exists anywhere in their table before giving it to them
if table.find(tableName, badgeName) ~= nil then
-- award badge
table.insert(tableName, badgeName)
end