Badge script should work, but doesn't?

So I have this badge script for when a player becomes a certain rank or above they get a badge for it, but it doesn’t work as I had hoped it would.

Help is greatly appreciated! :sweat_smile:

My Code
local GroupId = 4607825 --// My Group ID


local Player = game:GetService("Players").LocalPlayer

local BadgeService = game:GetService("BadgeService")


local MinRank = 232 --// Minimum rank that should get the badge
local BadgeId = 2124660106 --// My badge ID


if Player:GetRoleInGroup(GroupId) >= MinRank then --// If player is the minimum rank or above they should get the badge
	BadgeService:AwardBadge(Player.userId, BadgeId) --// Awards badge to player
	print("Badge Awarded to " .. Player.Name) --// Prints out who got the badge (for debugging purposes)
end

game:GetService(“Players”) returns the player service not the actual player, this would fix this issue

local GroupId = 4607825 --// My Group ID


local Player = game:GetService("Players").LocalPlayer

local BadgeService = game:GetService("BadgeService")


local MinRank = 232 --// Minimum rank that should get the badge
local BadgeId = 2124660106 --// My badge ID


if Player:GetRoleInGroup(GroupId) >= MinRank then --// If player is the minimum rank or above they should get the badge
	BadgeService:AwardBadge(Player.UserId, BadgeId) --// Awards badge to player
	print("Badge Awarded to " .. Player.Name) --// Prints out who got the badge (for debugging purposes)
end
2 Likes

It didn’t output anything… :confused:

char limitttt

Are you testing this in studio? If so try test it in game

I am testing in-game. No badge was awarded and nothing was in the dev console…

I haven’t worked with badge service in years so I’m gonna check the documentation

As @jake_4543 said,

The service Players is a service that contains all the players in your game, it doesn’t return you a local player.

Try this code :

local players = game:GetService("Players")
local badges = game:GetService("BadgeService")
local userHasBadge = badges.UserHasBadgeAsync
local awardBadge = badges.AwardBadge

local badgeId = 2124660106  
local groupId = 4607825 
local MinRank = 232 
	
local function onPlayerAdded(player)
	local success, result = pcall(userHasBadge, badges, player.UserId, badgeId) --HasAsync,Service,userID,badgeID
	if success then
		if not result then
			local success2, result2 = pcall(awardBadge, badges, player.UserId, badgeId)
			if success2 then
				
				if player:GetRoleInGroup(groupId) >= MinRank then
					badges:AwardBadge(player.UserId,badgeId)
				end
				if result2 then
					do end
				end
			else
				warn(result2)
			end
		end
	else
		warn(result)
	end
end

players.PlayerAdded:Connect(onPlayerAdded)

Ohh I forgot this has to be run on server, yeah I’m dumb lol

1 Like

If you want in a shorter way -

local players = game:GetService("Players")
local badges = game:GetService("BadgeService")
local userHasBadge = badges.UserHasBadgeAsync
local awardBadge = badges.AwardBadge

local badgeId = 2124660106  
local groupId = 4607825 
local MinRank = 232 



game.Players.PlayerAdded:Connect(function(Player)
	local success, result = pcall(userHasBadge, badges, Player.UserId, badgeId) --HasAsync,Service,userID,badgeID
	if success then
		if not result then
			if Player:GetRoleInGroup(groupId) >= MinRank then
				badges:AwardBadge(Player.UserId,badgeId)
			end
		end
	end
end)

Nothing was in output, and no badge was given…

Only the server can award badges.

Do you own the badge already? Or not?

No, I don’t own the badge.

char limittttt

I suggest you use an event to run a function that ultimately awards the badge.

I’m assuming you’re running this code in a server script, (it would not work in a local script anyways) so an easy fix would look something like this:

game.Players.PlayerAdded:Connect(function(Player)
     local GroupId = 4607825 --// My Group ID



     local BadgeService = game:GetService("BadgeService")


     local MinRank = 232 --// Minimum rank that should get the badge
     local BadgeId = 2124660106 --// My badge ID


     if Player:GetRoleInGroup(GroupId) >= MinRank then --// If player is the minimum rank or above they should get the badge
	     BadgeService:AwardBadge(Player.userId, BadgeId) --// Awards badge to player
	     print("Badge Awarded to " .. Player.Name) --// Prints out who got the badge (for debugging purposes)
     end
end

If you wish to run this code at another point in time other than when the player is joining, make sure the event returns the player’s instance so the script knows who to award the badge to.

Oh, my bad. The issue is here:

if success then
		if not result then
			if Player:GetRoleInGroup(groupId) >= MinRank then
				badges:AwardBadge(Player.UserId,badgeId)
			end
		end
	end

result here means that the player doesn’t have the badge, so I assume this needs to be re-written to :

game.Players.PlayerAdded:Connect(function(Player)
	local success, result = pcall(userHasBadge, badges, Player.UserId, badgeId) --HasAsync,Service,userID,badgeID
	if  result then
		if Player:GetRoleInGroup(groupId) >= MinRank then
			badges:AwardBadge(Player.UserId,badgeId)
		end
	end
end)

It’s always preferred to use pcalls when working with badges/gamepasses, etc. Simply because errors can occur.

If a user already owns a badge, attempting to award one will not return an error. Roblox’s servers cannot and will not attempt to award a badge twice. If they already own the badge, nothing will happen.

Doesn’t matter. You always need a pcall , since errors can easily occur in such cases.
Same goes with DataStore.

‘If a user already has a data, not using pcalls is okay’ - no, it’s not. It’s risky.

Tell me if it’s working, if not, I’ll check the code again

The badge still isn’t being awarded… :confused:

1 Like