How to award a badge if you own a gamepass?

So I want to make a script where if you own a gamepass you are awarded a badge once you join the game, how can I make that?

3 Likes
local Players = game:GetService('Players')
local BadgeService = game:GetService('BadgeService')
local MarketPlaceService = game:GetService('MaketPlaceService')

local GamepassId = 000000
local BadgeId = 000000

Players.PlayerAdded:Connect(function(Player)
    if MarketPlaceService:UserOwnsGamePassAsync(Player.UserId, GamepassId) then
        BadgeService:AwardBadge(Player.UserId, BadgeId)
    end
end)

If you run into any problems regarding this topic, reach out to me! :slightly_smiling_face:

7 Likes

To check if a user owns a gamepass use:
https://developer.roblox.com/en-us/api-reference/function/MarketplaceService/UserOwnsGamePassAsync

To award the badge:
https://developer.roblox.com/en-us/api-reference/function/BadgeService/AwardBadge

1 Like

can’t you just explain how to do it like what she’s asking for?

2 Likes

Slight spelling mistake on MarketplaceService.

2 Likes

Thank you so so much, it works perfectly! I was close to getting it right but I made a few mistakes in my script lol!

1 Like
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
local badgeId = 0

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
							print("Success!")
						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
							print("Success!")
						end
					else
						warn(result)
					end
				end
			else
				warn(result)
			end
		end
	end
end

players.PlayerAdded:Connect(onPlayerAdded)
marketplace.PromptGamePassPurchaseFinished:Connect(onGamePassPurchasePromptFinished)

In case you want an implementation which awards the badge when the gamepass is purchased (instead of the player needing to rejoin the game).

8 Likes

Thank you so much! This script looks really fun to learn on!