Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

How do you award a badge if a player owns a different badge?

Asked by 7 days ago
Edited 7 days ago

Hello, I am trying to award a badge to players who own a different badge (so if a player owns a badge in any other game you can award a badge in my game) However, when i join the game i dont get awarded the badge and there is no errors.

Here is my code

local BServ = game:GetService("BadgeService")
local BadgeOwned = 2124582050
local BadgeAward = 2124561788


game.Players.PlayerAdded(function(plr)
if BServ:UserHasBadgeAsync(plr.UserId, BadgeOwned) then
    BServ:AwardBadge(plr.UserId, BadgeAward)
end
end)
0
Is API services on Pupppy44 458 — 7d
0
yes Killerbot712 47 — 7d
0
Is it a Localscript? Xx_XSanderPlayXx 107 — 7d

1 answer

Log in to vote
0
Answered by 7 days ago
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")

local badgeOwned = 00000000
local badgeAward = 00000000

local function onPlayerAdded(player)
    -- Check if player has owned badge
    local success, hasBadge = pcall(function()
        return BadgeService:UserHasBadgeAsync(player.UserId, badgeOwned )
    end)

    -- if it trows an error, exit the function
    if not success then
        return
    end

    if hasBadge then
        -- Award badge
        local awarded, errorMessage = pcall(function()
            BadgeService:AwardBadge(player.UserId, badgeAward)
        end)
        if not awarded then
            warn("Error while awarding badge:", errorMessage)
        end
    end
end

-- Connect event PlayerAdded with OnPlayerAdded function
Players.PlayerAdded:Connect(onPlayerAdded)

Hope it helps!

0
Works thanks! Killerbot712 47 — 7d
Ad

Answer this question