Badge Not Working

Info:
I have a badge create by my friend, it awards the badge when you join the game (like those Welcome Badges)
Script:

local BadgeService = game:GetService("BadgeService")
local badgeID = 2124596471 
local function awardBadge(player, badgeId)
	local success, badgeInfo = pcall(function()
		return BadgeService:GetBadgeInfoAsync(badgeId)
	end)
	if success then
	if badgeInfo.IsEnabled then
			local awarded, errorMessage = pcall(function()
				BadgeService:AwardBadge(player.UserId, badgeId)
				print('Awarded')
			end)
			if not awarded then
	warn("Error while awarding badge:", errorMessage)
		end	end
	else warn("Error while fetching badge info!") end
end
 
local function onPlayerAdded(player)
	player.CharacterAdded:Wait()
	wait(3)
	-- 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 not hasBadge then
		awardBadge(player, badgeID)
	end
end
 
-- Connect "PlayerAdded" events to the "onPlayerAdded()" function
game.Players.PlayerAdded:Connect(onPlayerAdded)
while wait(10) do
for i,v in pairs(game.Players:GetPlayers()) do
		local success, hasBadge = pcall(function()
		return BadgeService:UserHasBadgeAsync(v.UserId, badgeID)
	end)print('Does the player own the Welcome Badge?: '..tostring(hasBadge))
end
end

Sorry for the messy script. When a user joins, it checks to make sure the player does not have the badge, then awards the badge. After it’s awarded, ‘Awarded’ will be printed. Also every 10 seconds, it will check if the player owns the badge. This is the output:

Awarded
Does the player own the Welcome Badge?: false

Apparently it awarded the badge but the player doesn’t own it. I also checked my inventory and confirmed that I didn’t get the badge.

it awards the badge when you join the game (like those Welcome Badges)

This is certainly a bit over-kill, you’re checking everytime a player respawns, and even in a loop every 10 seconds.

If anything, it takes up unnecessary space and bandwith.
You can just check everytime they join, no?

Here’s an example:

local Players = game:GetService('Players')
local BadgeService = game:GetService('BadgeService')
local Id = 2124596471

Players.PlayerAdded:Connect(function(Player)
	local Ok, Status = pcall(function()
		return BadgeService:UserHasBadgeAsync(Player.UserId, Id)
	end)
	
	if not Ok then
		print('Error while checking if player owns the badge!')
		return
	end
	
	if not Status then
		-- Award them the badge.
	else
		-- They do not own the badge.
	end
end)

Ok is the response wether the call was successful or not.
Status is what is returned if they own the badge or not, either true or false.

I’m not sure if this is correct, but I test-played twice, both times having Awarded! in the Output, and the badge is definitely not in my inventory.

local Players = game:GetService('Players')
local BadgeService = game:GetService('BadgeService')
local Id = 2124596471
local function awardBadge(player, badgeId)
	local success, badgeInfo = pcall(function()
		return BadgeService:GetBadgeInfoAsync(badgeId) -- This makes sure the badge is enabled in case I disable it.
	end)
	if success then
	if badgeInfo.IsEnabled then
			local awarded, errorMessage = pcall(function()
				BadgeService:AwardBadge(player.UserId, badgeId)
				print('Awarded') -- This is printed after the badge is awarded
			end)
			if not awarded then
	warn("Error while awarding badge:", errorMessage)
		end	end
	else warn("Error while fetching badge info!") end
end
 

Players.PlayerAdded:Connect(function(Player)
	local Ok, Status = pcall(function()
		return BadgeService:UserHasBadgeAsync(Player.UserId, Id) -- This returns false if player doesn't have badge
	end)
	
	if not Ok then
		print('Error while checking if player owns the badge!')
		return
	end
	
	if not Status then
		awardBadge(Player, Id) -- The player will be awarded a badge if he/she doesn't have the badge
	else
		print('This person has the badge!') -- This prints if he/she had the badge.
	end
end)
1 Like

You should test this in a live server. I don’t believe you can be awarded with the badge in studio.

1 Like