You can write your topic however you want, but you need to answer these questions:
What do you want to achieve?
I want to make a badge that people get for joining my game for the first time.
What is the issue?
In game, on the developer console, it says “Failed to award badge”, the script has no errors and the badge is active
What solutions have you tried so far?
I have tried different methods of awarding the badge and checking but it doesn’t work.
My code, in ServerSciptService, the code is from BadgeService | Roblox Creator Documentation so I don’t understand why it doesn’t work, I did create my own script initially but it didn’t work so I tried Roblox’s own script.
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local BADGE_ID = 2129936701
local function awardBadge(player, badgeId)
-- Fetch badge information
local success, badgeInfo = pcall(function()
return BadgeService:GetBadgeInfoAsync(badgeId)
end)
if success then
-- Confirm that badge can be awarded
if badgeInfo.IsEnabled then
-- Award badge
local awardSuccess, result = pcall(function()
return BadgeService:AwardBadge(player.UserId, badgeId)
end)
if not awardSuccess then
-- the AwardBadge function threw an error
warn("Error while awarding badge:", result)
elseif not result then
-- the AwardBadge function did not award a badge
warn("Failed to award badge.")
end
end
else
warn("Error while fetching badge info: " .. badgeInfo)
end
end
local function onPlayerAdded(player)
awardBadge(player, BADGE_ID)
end
Players.PlayerAdded:Connect(onPlayerAdded)
– SERVICES
local BadgeService = game:GetService(“BadgeService”)
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
– EVENTS
local Network = ReplicatedStorage.Events:WaitForChild(“Network”)
local Update = ReplicatedStorage.Events:WaitForChild(“Update”)
– PLAYER VARIABLES
local StarterGui = game:GetService(“StarterGui”)
local Players = game:GetService(“Players”)
local Player = Players.LocalPlayer
local PlayerGUI = Player:WaitForChild(“PlayerGui”)
local Client = PlayerGUI:WaitForChild(“Client”)
– VARIABLES
local BadgeContainer = script.Parent.Parent.BadgesFrame:WaitForChild(“Container”)
local Badges = {2130459377}
– FUNCTIONS
function BadgesService:CreateBadge(badgeId, state)
local badgeObj = script:WaitForChild(“BadgeObj”):Clone()
local success, result = pcall(function()
return BadgeService:GetBadgeInfoAsync(badgeId)
end)
if not success then
return warn("[CLIENT] - Could not load badge!")
end
badgeObj.Name = result.Name
if state then
badgeObj.Title.Text = result.Name
badgeObj.Description.Text = result.Description
badgeObj.Info.State.Text = “Owned”
else
badgeObj.Title.Text = result.Name
badgeObj.Description.Text = result.Description
badgeObj.Info.State.Text = “Not Owned”
end
badgeObj.Parent = BadgeContainer
end
function BadgesService:LoadBadges()
for _, badgeID in pairs(Badges) do
if Network:InvokeServer(“Badge”, badgeID) then
BadgesService:CreateBadge(badgeID, true)
else
BadgesService:CreateBadge(badgeID, false)
end
end
end
you dont have to check if the badge is enabled since its already pcalled
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local BADGE_ID = 2129936701
local function awardBadge(player, badgeId)
local awardSuccess, result = pcall(function()
return BadgeService:AwardBadge(player.UserId, badgeId)
end)
if not awardSuccess then
-- the AwardBadge function threw an error
warn("Error while awarding badge:", result)
elseif not result then
-- the AwardBadge function did not award a badge
warn("Failed to award badge.")
end
end
local function onPlayerAdded(player)
awardBadge(player, BADGE_ID)
end
Players.PlayerAdded:Connect(onPlayerAdded)
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local BADGE_ID = 2129936701
local function awardBadge(player, BADGE_ID)
-- Fetch badge information
local success, badgeInfo = pcall(function()
return BadgeService:GetBadgeInfoAsync(BADGE_ID)
end)
if success then
-- Confirm that badge can be awarded
if badgeInfo.IsEnabled then
-- Award badge
local awardSuccess, result = pcall(function()
return BadgeService:AwardBadge(player.UserId, BADGE_ID)
end)
if not awardSuccess then
-- the AwardBadge function threw an error
warn("Error while awarding badge:", result)
elseif not result then
-- the AwardBadge function did not award a badge
warn("Failed to award badge.")
end
end
else
warn("Error while fetching badge info: " .. badgeInfo)
end
end
local function onPlayerAdded(player)
awardBadge(player, BADGE_ID)
end
Players.PlayerAdded:Connect(onPlayerAdded)
Does the badge ID that you are attempting to award belong to a badge that you made yourself? You can’t award badges that don’t belong to the game you are trying to award it in.
Edit: You can create badges for free by going to the game settings in studio.