Badge service script not working

Hello I am learning to use this service trying to make it give me a medal if it finds my name, but the script does not work, does anyone know what is wrong?

The output shows nothing

local badgeService = game:GetService("BadgeService")
local badgeId = 2463675936
local players = game:GetService("Players")

for _, player in pairs(players:GetPlayers()) do
	if player.Name == 'MatiasHarders' then
		badgeService:AwardBadge(player.UserId, badgeId)
	end
end

Try switching to PlayerAdded event

1 Like

Try this and tell me what it prints:


local badgeInfo = badgeService:GetBadgeInfoAsync(badgeId)

for _, player in pairs(players:GetPlayers()) do
	if player.Name == 'MatiasHarders' then
        print(badgeInfo.IsEnabled)
		badgeService:AwardBadge(player.UserId, badgeId)
	end
end

Edit:
I am pretty sure this will work because I got it straight off of the wiki.

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

local function awardBadge(player, badgeId)
	-- Fetch badge information
	local success, badgeInfo = pcall(function()
		return BadgeService:GetBadgeInfoAsync(badgeId)
	end)
	if success then
		-- Confirm that badge can be awarded
		if badgeInfo.IsEnabled then
			-- Award badge
			local awarded, errorMessage = pcall(function()
				BadgeService:AwardBadge(player.UserId, badgeId)
			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)
	if player.Name == 'MatiasHarders' then
        awardBadge(player, badgeId)
	end
end)
1 Like

Another thing that might be happening is that the player has not loaded in yet when you are trying to find them. As @c_PRO said, it is best to use the PlayerAdded event.

1 Like

Badges are a whole different thing, you are trying to use ID of this (Cryptid Hunter Badge), which is an accessory, badges are these things

(which are shown under the game’s social links)

1 Like

Should I create the medal myself to use it?

Badges are virtual awards you can give to people in-game using the BadgeService, you, however are trying to use the service to award an item (trying to make yourself equip it???), which isn’t how it works: if you don’t understand what badges are, this page should explain it

1 Like

Yes, you do have to create your own badge to use it in your own server. Which costs 100 Robux :slight_smile:

1 Like