Badge not giving even though the script is 100% foolproof

Hi, so for some reason I created this script where if a object is touched it gives a player a badge:

local badgeService = game:GetService("BadgeService")
local badgeId = 1022294272051045

local part = script.Parent

local function onTouched(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		local success, hasBadge = pcall(function()
			return badgeService:UserHasBadgeAsync(player.UserId, badgeId)
		end)

		if success and not hasBadge then
			pcall(function()
				badgeService:AwardBadge(player.UserId, badgeId)
				print("Badge awarded to " .. player.Name)
			end)
		end
	end
end

part.Touched:Connect(onTouched)

(Yes i got this from chatgpt)
I have tried looking up tutorials, looking at other forums, and even asking chatgpt, but nothing has worked…
unfortunately i cant upload anything because i dont know how too

Anything helps!

This may help you. It has everything about how to make a player award some badge.

Oh, how would i upload a badge then? Whenever i copy the badgeid it gives me this number.

try printing: BadgeService:GetBadgeInfoAsync(BadgeID). If it returns the info, the id is valid.

This is what looks wrong to me… can you post the badge URL.
(keep in mind I guess until I find the problem, when I don’t see it right away)
Should be able to go to that URL and copy the the badgeID.

This is the badge id

nvm that’s the game’s id…

This text will be blurred

I was just going to say that looks like a game ID

1 Like

The badge ID is supposedly not the problem then; try switching your code to this:

local badgeService = game:GetService("BadgeService")
local badgeId = 1022294272051045

local part = script.Parent

local function onTouched(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		local success, info = pcall(function()
			return badgeService:GetBadgeInfoAsync(badgeId)
		end)

        print(info.IsEnabled)

		if success and info.IsEnabled then
			pcall(function()
				badgeService:AwardBadge(player.UserId, badgeId)

				print("Badge awarded to " .. player.Name)
			end)
		end
	end
end

part.Touched:Connect(onTouched)

I do not have the badge
text blurred

To get the badge id you can go to: Creator Hub > The game you want > Engagement > Badges > Hover over the badge you want > Click the 3 dots in the top right corner of the badge icon > Click “Copy asset ID”

This code still doesnt work, and its not just the badge, ive had previous attempts trying to give a player a badge and nothing has worked, i previously assumed it depended on how far out you were, but that isnt the case

1 Like

Yes, i did that, i assure you its not the badge id, im pretty sure there is something wrong with the game itself.

Here is one of mine…

local function AwardBadge(player, badge)
	local success, hasBadge = pcall(function()
		return BadgeService:UserHasBadgeAsync(player.UserId, badge)
	end)
	if not success then
		warn("Error while checking if player has badge!")
		return
	end

	if not hasBadge then
		local success, badgeInfo = pcall(function()
			return BadgeService:GetBadgeInfoAsync(badge)
		end)
		if success then
			if badgeInfo.IsEnabled then
				local awarded, errorMessage = pcall(function()
					BadgeService:AwardBadge(player.UserId, badge)
				end)
				if not awarded then
					warn("Error while awarding badge:", errorMessage)
				else
					print("Awarding badge")
				end
			end
		else
			warn("Error while fetching badge info!")
		end
	end
end

Been working fine for a few years now.

1 Like

Yeah it still doesnt work, also ive realized that the badgegivers in the toolbox dont work either, all the (badge giving) scripts dont give errors, or warnings. they just dont work…

try printing if the badge is valid?

Yes, i just did it, And it proved it valid… Just a bug i guess

Well that isn’t good. May have to leave it for a bit… I’m more than sure the script I posted works.

I was thinking just migrating the whole game to a different place, such as it doesnt work at all.

Let me check If I can get my badge… I just checked and my badge didn’t work either.
I know for sure that works so… They changed something or you will have to wait a bit. They may be working on something to do with this right now. I’ll just wait and check it later.

I also checked your ID via URL and it’s there by that ID … 1022294272051045