Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How To Make It So When You Join The Game It Awards You A Badge?

Asked by 4 years ago

I've Tried Searching All Over The Web And This Is The Best Thing I Found.

on.PlayerJoined then
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")

local badgeID = 2124488164  -- Change this to your badge ID

local function awardBadge()

    local player = Players.LocalPlayer
    local hasBadge = false

    -- Check if the player already has the badge
    local success, message = pcall(function()
        hasBadge = BadgeService:UserHasBadgeAsync(player.UserId, badgeID)
    end)

    -- If there's an error, issue a warning and exit the function
    if not success then
        warn("Error while checking if player has badge: " .. tostring(message))
        return
    end

    if hasBadge == false then
        BadgeService:AwardBadge(player.UserId, badgeID)
    end
end

1 answer

Log in to vote
1
Answered by
Vathriel 510 Moderation Voter
4 years ago
Edited 4 years ago

You're on the right track. However, your awardBadge() function is never called!

This can be fixed by adding

awardBadge()

to the bottom of the script.

Another problem is that this uses LocalPlayer, which is not available on the server, but according to the wiki you may only award badges via the server.

To fix this you might consider a solution like the following:

local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")

local badgeID = 2124488164  -- Change this to your badge ID
local function awardBadge(player)
    local hasBadge = false

    -- Check if the player already has the badge
    local success, message = pcall(function()
        hasBadge = BadgeService:UserHasBadgeAsync(player.UserId, badgeID)
    end)

    -- If there's an error, issue a warning and exit the function
    if not success then
        warn("Error while checking if player has badge: " .. tostring(message))
        return
    end

    if hasBadge == false then
        BadgeService:AwardBadge(player.UserId, badgeID)
    end
end
game.Players.PlayerAdded:connect(awardBadge)

I should also note that you incorrectly connected the event. Find out more about events here:

https://developer.roblox.com/en-us/articles/events

0
Thank You So Much ricelicker 52 — 4y
Ad

Answer this question