Badges not being awarded after player reaches value needed

Hello. I’m making a system where I award a player whenever they reach a milestone of a value. Issue is that it only awards the player for the first badge and the not the rest. How can I go about fixing this?

local badgeService = game:GetService("BadgeService")
local EachMileStone = {
	[1] = 2149272327,
	[100] = 2149272333, 
	[1000] = 2149272344,
	[10000] = 2149272355,
	[100000] = 2149272400,
}
local DoTheyHaveThisBadgeAlready = {}
game.Players.PlayerAdded:Connect(function(player)
	repeat task.wait() until player:FindFirstChild("leaderstats") ~= nil 
	player.leaderstats.Bricks.Changed:Connect(function()
		for i, v in EachMileStone do 
			if player.leaderstats.Bricks.Value >= i and table.find(DoTheyHaveThisBadgeAlready, v) == nil then
				if not badgeService:UserHasBadgeAsync(player.UserId, v) then
					print(v)
					badgeService:AwardBadge(player.UserId, v)
					
				end
				table.insert(DoTheyHaveThisBadgeAlready, v)
			elseif player.leaderstats.Bricks.Value < i then 
				return
			end
		end
	end)
end)

It only outputs the first badge assetId and nothing else.
Any and all help is very much appreciated.

Hello!

If I’m not mistaken, I believe AwardBadge only needs the Player argument rather than Player.UserId.

Maybe try that! (UserHasBadgeAsync still needs Player.UserId)

EDIT: Also you forgot pairs or ipairs in the for loop!

It’s still printing the first badge assetId.

Did you update the for loop to in pairs too?

Yes.

local badgeService = game:GetService("BadgeService")
local EachMileStone = {
	[1] = 2149272327,
	[100] = 2149272333, 
	[1000] = 2149272344,
	[10000] = 2149272355,
	[100000] = 2149272400,
}
local DoTheyHaveThisBadgeAlready = {}
game.Players.PlayerAdded:Connect(function(player)
	repeat task.wait() until player:FindFirstChild("leaderstats") ~= nil 
	player.leaderstats.Bricks.Changed:Connect(function()
		for i, v in pairs(EachMileStone) do 
			if player.leaderstats.Bricks.Value >= i and table.find(DoTheyHaveThisBadgeAlready, v) == nil then
				if not badgeService:UserHasBadgeAsync(player.UserId, v) then
					print(v)
					badgeService:AwardBadge(player.UserId, v)
				end
				table.insert(DoTheyHaveThisBadgeAlready, v)
			elseif player.leaderstats.Bricks.Value < i then 
				return
			end
		end
	end)
end)

I had to keep player.UserId in AwardBadge since it’d throw up a “can’t cast instance to int64” error or something like that. Could it be because I’m using a difference place to test it than the place where the badges are?

Hey!

I wrote something up for you, during initial testing before I added the leaderstats stuff this was printing all badge IDs, I probably messed something up but try this.

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

local EachMileStone = {
	[2149272327] = 1,
	[2149272333] = 100, 
	[2149272344] = 1000,
	[2149272355] = 10000,
	[2149272400] = 100000,
}

PlayerService.PlayerAdded:Connect(function(Player)
	repeat task.wait(0.5) until Player:FindFirstChild("leaderstats") ~= nil;
	Player.leaderstats.Bricks.Changed:Connect(function()
		for BadgeIds, Values in pairs(EachMileStone) do
			if Player.leaderstats.Bricks.Value >= Values then
				if not badgeService:UserHasBadgeAsync(Player.UserId, BadgeIds) then
					print(BadgeIds)
					badgeService:AwardBadge(Player.UserId, BadgeIds)
				end
			end
		end
	end)
end)
1 Like

This works. Thank you so much! Have a good day.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.