It may be a little wrong, to be honest I don’t know, but the main problem bothers me a lot, how can it be fixed?
I keep getting UserHasBadgeAsync errors, my third thread on the same topic. Can it be fixed with pcall? Even if it is fixed with pcall, all my badges have pcall in it, does this provide a negative reaction time? I need help
Serverside script:
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local ChatService = require(game.ServerScriptService:WaitForChild("ChatServiceRunner"):WaitForChild("ChatService"))
local BadgeService = game:GetService("BadgeService")
local BadgeId = 2127223808
local BadgeId2 = 2127419839
local gamepassId = 27520453
local groupId = 3687166
ChatService.SpeakerAdded:Connect(function(username)
local speaker = ChatService:GetSpeaker(username)
local player = Players:FindFirstChild(username)
local gamepassOwned = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamepassId)
local inGroup = player:IsInGroup(groupId)
local tags = {}
if gamepassOwned then
table.insert(tags, {TagText = "⭐", TagColor = Color3.fromRGB(255, 200, 61)})
end
if inGroup then
local role = player:GetRoleInGroup(groupId)
table.insert(tags, {TagText = role, TagColor = Color3.fromRGB(0, 255, 77)})
end
if BadgeService:UserHasBadgeAsync(player.UserId,BadgeId2) then
table.insert(tags, {TagText = "💸", TagColor = Color3.fromRGB(95, 178, 65)})
end
if BadgeService:UserHasBadgeAsync(player.UserId,BadgeId) then
table.insert(tags, {TagText = "🍫", TagColor = Color3.fromRGB(221, 46, 68)})
end
speaker:SetExtraData("Tags", tags)
end)
If you don’t wrap this in a pcall, there will be a 0.01% of badgeservice failing, so you should wrap it in a pcall.
How to do it
In game settings(security) enable studio access to API services
Make each badge an individual script
Gamepass badge (awarded for purchasing a gamepass):
local players = game:GetService("Players")
local marketplace = game:GetService("MarketplaceService")
local userOwnsGamePass = marketplace.UserOwnsGamePassAsync
local badges = game:GetService("BadgeService")
local userHasBadge = badges.UserHasBadgeAsync
local awardBadge = badges.AwardBadge
local gamePassId = 0 --Change to ID of gamepass.
local badgeId = 0 --Change to ID of badge.
local function onPlayerAdded(player)
local success, result = pcall(userOwnsGamePass, marketplace, player.UserId, gamePassId)
if success then
if result then
local success2, result2 = pcall(userHasBadge, badges, player.UserId, badgeId)
if success2 then
if not result2 then
local success3, result3 = pcall(awardBadge, badges, player.UserId, badgeId)
if success3 then
if result3 then
do end
end
else
warn(result3)
end
end
else
warn(result2)
end
end
else
warn(result)
end
end
local function onGamePassPurchasePromptFinished(player, _gamePassId, wasPurchased)
if wasPurchased then
if _gamePassId == gamePassId then
local success, result = pcall(userHasBadge, badges, player.UserId, badgeId)
if success then
if not result then
local success2, result2 = pcall(awardBadge, badges, player.UserId, badgeId)
if success2 then
if result2 then
do end
end
else
warn(result2)
end
end
else
warn(result)
end
end
end
end
players.PlayerAdded:Connect(onPlayerAdded)
marketplace.PromptGamePassPurchaseFinished:Connect(onGamePassPurchasePromptFinished)
Sometimes, badgeservice has a 0.01% of failing. A pcall can help.
How to make a errorMessage:
After the if statement that will award you the badge, type this in your code
local awarded, errorMessage = pcall(function()
Now, award you badge:
local awarded, errorMessage = pcall(function()
BadgeService:AwardBadge(player.UserId, BadgeId)
end)
Now, make your error message
local awarded, errorMessage = pcall(function()
BadgeService:AwardBadge(player.UserId, BadgeId)
end)
if not awarded then
warn("Error while awarding badge:", errorMessage)
end
end
end)
a pcall can help a lot when dealing with badgeservice promblems
you can do a lot with pcall because they are functions that don't error and make
your code run successfully.
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local ChatService = require(game.ServerScriptService:WaitForChild("ChatServiceRunner"):WaitForChild("ChatService"))
local BadgeService = game:GetService("BadgeService")
local BadgeId = 2127223808
local BadgeId2 = 2127419839
local gamepassId = 27520453
local groupId = 3687166
ChatService.SpeakerAdded:Connect(function(username)
local speaker = ChatService:GetSpeaker(username)
local player = Players:FindFirstChild(username)
local gamepassOwned = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamepassId)
local inGroup = player:IsInGroup(groupId)
local tags = {}
if gamepassOwned then
table.insert(tags, {TagText = "⭐", TagColor = Color3.fromRGB(255, 200, 61)})
end
if inGroup then
local role = player:GetRoleInGroup(groupId)
table.insert(tags, {TagText = role, TagColor = Color3.fromRGB(0, 255, 77)})
end
local status, data = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId,BadgeId2)
end)
if not status then error(data) end
if data then
table.insert(tags, {TagText = "💸", TagColor = Color3.fromRGB(95, 178, 65)})
end
status, data = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId,BadgeId)
end)
if not status then error(data) end
if data then
table.insert(tags, {TagText = "🍫", TagColor = Color3.fromRGB(221, 46, 68)})
end
speaker:SetExtraData("Tags", tags)
end)