How do I award badges after value is reached?

Hello. I’m making a system that awards the player after reaching a certain number of a value inside of leaderstats. Issue is that it’s throwing up an error that says that leaderstats isn’t a child of player. Should i just add a :waitforchild? What’s the issue?

local badgeService = game:GetService("BadgeService")

game.Players.PlayerAdded:Connect(function(player)
	if player:WaitForChild("leaderstats") then
	player.leaderstats.Bricks.Changed:Connect(function()
		local function awardBadge(player, badgeId)
			if player.leaderstats.Bricks.Value >= 1 then
				if not badgeService:UserHasBadgeAsync(player.UserId, 2149272327) then
					badgeService:AwardBadge(player.UserId, 2149272327)
				end
			if player.leaderstats.Bricks.Value >= 100 then
					if not badgeService:UserHasBadgeAsync(player.UserId,2149272333) then
						badgeService:AwardBadge(player.UserId, 2149272333)
					end
				end
			end
			if player.leaderstats.Bricks.Value >= 1000 then
				if not badgeService:UserHasBadgeAsync(player.UserId, 2149272344) then
					badgeService:AwardBadge(player.UserId, 2149272344)
				end
			end
			if player.leaderstats.Bricks.Value >= 10000 then
				if not badgeService:UserHasBadgeAsync(player.UserId, 2149272355) then
					badgeService:AwardBadge(player.UserId, 2149272355)
				end
			end
			if player.leaderstats.Bricks.Value >= 100000 then
				if not badgeService:UserHasBadgeAsync(player.UserId, 2149272400) then
					badgeService:AwardBadge(player.UserId, 2149272400)
				end
			end
			end
		end)
	end
end)

Any help is very much appreciated. Thanks.

Do you create the leader stats folder in any other script?

Yes. Inside of my datastore script. It gets created whenever the player joins. But that shouldn’t be an issue if I just re-run the script right? The current script doesn’t even run at all… but without the :waitforchild line it throws up the leaderstat error.

Try this

game.Players.PlayerAdded:Connect(function(player)
repeat task.wait() until player:FindFirstChild("leaderstats") ~= nil 
player.leaderstats.Bricks.Changed:Connect(function()
--rest of code 

1 Like

Could it also be because I’m running the script on a test place and not the actual game with the badges? Should I just add print statements instead?

Shouldn’t matter as long as your making the leaderstats folder, (as the error is from the leaderstats folder). Just be aware you don’t get the badges if they aren’t owned by the same universe, and those lines, will throw errors

Also you have a local function but your not calling it at all. Just remove it, like this

local badgeService = game:GetService("BadgeService")

game.Players.PlayerAdded:Connect(function(player)
	repeat task.wait() until player:FindFirstChild("leaderstats") ~= nil 
	player.leaderstats.Bricks.Changed:Connect(function()
		
			if player.leaderstats.Bricks.Value >= 1 then
				if not badgeService:UserHasBadgeAsync(player.UserId, 2149272327) then
					badgeService:AwardBadge(player.UserId, 2149272327)
				end
			if player.leaderstats.Bricks.Value >= 100 then
					if not badgeService:UserHasBadgeAsync(player.UserId,2149272333) then
						badgeService:AwardBadge(player.UserId, 2149272333)
					end
				end
			end
			if player.leaderstats.Bricks.Value >= 1000 then
				if not badgeService:UserHasBadgeAsync(player.UserId, 2149272344) then
					badgeService:AwardBadge(player.UserId, 2149272344)
				end
			end
			if player.leaderstats.Bricks.Value >= 10000 then
				if not badgeService:UserHasBadgeAsync(player.UserId, 2149272355) then
					badgeService:AwardBadge(player.UserId, 2149272355)
				end
			end
			if player.leaderstats.Bricks.Value >= 100000 then
				if not badgeService:UserHasBadgeAsync(player.UserId, 2149272400) then
					badgeService:AwardBadge(player.UserId, 2149272400)
				end
			end
			end
		
	end
end)
1 Like

And because I’m bored I made a nicer verision of this script thats easier to read like this :slight_smile: :

local badgeService = game:GetService("BadgeService")
local EachMileStone = {
[1] = 2149272327,
[100] = 2149272333, 
[1000] = 2149272344,
[10000] = 2149272355,
[100000] = 2149272400,
}

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 then
				if not badgeService:UserHasBadgeAsync(player.UserId, v) then
					badgeService:AwardBadge(player.UserId, v)
				end
            end 
        end
			
	end)
		
	
end)

Let me know if this works! Edit: Fixed a Missing end )

1 Like

Please don’t check if a user owns a badge multiple times every time the value is changed

Awesome! Thanks for your help. One note though. I added print functions to see what badge would be given and it just prints every badge that the player can get every time the value changes. Which is technically what I want but is there a way for me to stop that or is it gonna be just fine with the BadgeService and it is just the print statements?

Yup, we will just add a debounce by using a list

1 Like

We can use something like this to make it work!

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 ipairs(EachMileStone) do 
                      if player.leaderstats.Bricks.Value >= i and table.find(DoTheyHaveThisBadgeAlready, v) == nil then
				if not badgeService:UserHasBadgeAsync(player.UserId, v) then
					badgeService:AwardBadge(player.UserId, v)

				end
  table.insert(DoTheyHaveThisBadgeAlready, v)
elseif player.leaderstats.Bricks.Value < i then 
       return
            end 
        end
			
	end)
		
	
end)
1 Like

First print might print more then one, as a check, but the next prints should be ok

1 Like

First print prints just the right badge. Every other value change doesn’t print anything. 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.