How do I tell if a player gets a badge?

All I want to do is make it so when a player gets a badge I can do whatever

6 Likes

I found this on the roblox document:

local BadgeService = game:GetService(“BadgeService”)
local Players = game:GetService(“Players”)

local badgeId = 00000000 – Change this to your badge ID

local function onPlayerAdded(player)
– Check if the player has the badge
local success, hasBadge = pcall(function()
return 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!”)
return
end

if hasBadge then
– Handle player’s badge ownership as needed
print(player.Name, “has badge”, badgeId)
end
end

– Connect “PlayerAdded” events to the “onPlayerAdded()” function
Players.PlayerAdded:Connect(onPlayerAdded)

(The actual document:)

2 Likes

Not sure if this is what you’re talking about … This script on spawns checks if the player has a “blue pass” then gives them extra items and updates the buy gui menu to show they have the pass.

-- server script in ServerScriptService 

local Players = game:GetService("Players")
local MarketPlaceService = game:GetService("MarketplaceService")
local gamepassID = 123456789  -- BluePass 

function Spawned(player)
	task.wait(1.1) local HasGamepass = false
	local success, message = pcall(function()
		HasGamepass = MarketPlaceService:UserOwnsGamePassAsync(player.userId, gamepassID)
	end)
	
	if not success then
		warn("Checking Gamepass "..tostring(message))
		return
	end
	
	if HasGamepass == true then	-- print("Adding BlueGamePass")
		game.ServerStorage.Tools.SpeedDrink2:Clone().Parent = player.Backpack
		player.PlayerGui.ShopGui.Frame.Frame_1.Gamepass1.TextLabel.Visible = true
		player.PlayerGui.ShopGui.Frame.Frame_1.Gamepass1.Own.Value = true
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		Spawned(player)
	end)
end)

1 Like

Sorry about that last post … that is for game passes.
This one is a badge check.

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

Players.PlayerAdded:Connect(function(player)
	local badgeId = 123456789 -- Replace with the badge ID you want to check

	local success, hasBadge = pcall(function()
		return BadgeService:UserHasBadgeAsync(player.UserId, badgeId)
	end)

	if success and hasBadge then
		print(player.Name .. " has the badge with ID " .. badgeId)
	else
		print(player.Name .. " does not have the badge with ID " .. badgeId)
	end
end)```
1 Like

You can make an event and use the BadgeService:UserHasBadgeAsync to check whether or not if the user owns the badge. For example:

game.Players.PlayerAdded:Connect(function(Player)
   -- // Make a call that runs regardless if the scripts throws an error

   local success, errorMessage = pcall(function()
        -- // Checks if the scripts throws and error, and make it print once it’s clear
end

   if success then
       local BadgeService = game:GetService(“BadgeService”)
         if BadgeService:UserHasBadgeAsync(Player.UserId, 123456) then
           -- // Check the user if they own it
         else
             -- // If the user doesn’t own it, show it
         end
   else
        print(“An error occurred”)
        warn(errorMessage)
   end
end
1 Like

All of these just see if a player has a badge, I want to see if a player gets a badge

1 Like

If you want something to happen when a badge is rewarded, you’ll need to execute that event using BadgeService:AwardBadge(), like this:

BadgeService:AwardBadge(UserId, BadgeId)
print("Badge Awarded")

Additionally, you can link that into an event, like this:

-- // Variables
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")

local badgeId = 12345678

-- // Main Script
Players.PlayerAdded:Connect(function(Player)
	if BadgeService:UserHasBadgeAsync(Player.UserId, badgeId) then
		-- // Make something happen if the user has the badge
	else
		local success, errorMessage = pcall(function()
			-- // If the pcall is a success, and no errors are occurring, you can make a statement in the console here
		end)
		
		if success then
			-- // Award the badge
			-- // If you want something to happen after it's been given, use that here
		else
			print("An error occurred, see below for details")
			warn(errorMessage)
		end
	end
end)