How do I get Multiple Leaderstats Badges to work?

Hi! I’m very new to scripting in Roblox and I got stuck on this one issue I’ve been trying to resolve.

I made a Server-Side Script inside the ServerScriptService property, where once the player is added it will check to see if it had reached to a certain point of leaderstats value (in this case I’m using Level as my leaderstats value). Basically what I want is when the player reaches level 5, 10, 25, 50, 75, 100, and 1000, they will be rewarded with a unique badge as an accomplishment.

The PROBLEM is that when they level up to 5 they get a badge, but after they level up to 10, 25, 50, 75, 100, or even 1000, the script won’t award the player those badges to them for some reason.

Now, I tried to combat this issue by adding a then return end statement (meaning if they already have this badge then it will not give them this badge again) before they get the badge award, but it still didn’t do anything.

I also tried troubleshooting by using print, but all the output said was “got lvl 5 badge” even though I was already above Level 5 and even when I was Level 10.

MY GOAL: I want the player and other players be awarded with all the other level up badges.

This is my Code from my Sever-Side Script inside of ServerScriptService:

local BadgeService = game:getService("BadgeService") 

game.Players.PlayerAdded:Connect(function(player)

	local ls = player:WaitForChild("leaderstats")
	local lvl = ls.Level

	lvl:GetPropertyChangedSignal("Value"):Connect(function()
		if lvl.Value >= 5 then
			if BadgeService:UserHasBadgeAsync(player.UserId, 2149272660) then return end
			BadgeService:AwardBadge(player.UserId, 2149272660)
			print("got lvl 5 badge")
		elseif lvl.Value >= 10 then
			if BadgeService:UserHasBadgeAsync(player.UserId, 2149272708) then return end
			BadgeService:AwardBadge(player.UserId, 2149272708)
			print("got lvl 10 badge")
		elseif lvl.Value >= 25 then
			if BadgeService:UserHasBadgeAsync(player.UserId, 2149272758) then return end
			BadgeService:AwardBadge(player.UserId, 2149272758)
			print("got lvl 25 badge")
		elseif lvl.Value >= 50 then
			if BadgeService:UserHasBadgeAsync(player.UserId, 2149272786) then return end
			BadgeService:AwardBadge(player.UserId, 2149272786)
			print("got lvl 50 badge")
		elseif lvl.Value >= 75 then
			if BadgeService:UserHasBadgeAsync(player.UserId, 2149272825) then return end
			BadgeService:AwardBadge(player.UserId, 2149272825)
			print("got lvl 75 badge")
		elseif lvl.Value >= 100 then
			if BadgeService:UserHasBadgeAsync(player.UserId, 2149272859) then return end
			BadgeService:AwardBadge(player.UserId, 2149272859)
			print("got lvl 100 badge")
		elseif lvl.Value >= 1000 then
			if BadgeService:UserHasBadgeAsync(player.UserId, 2149272877) then return end
			BadgeService:AwardBadge(player.UserId, 2149272877)
			print("got lvl 1000 badge")
		end
	end)
end)
1 Like

You’ll have to specify that the value should be between 5 and 10 to recieve a badge for the first one and so on for all the other if statements.

1 Like

Can you explain more? Like can you show me a line of code? I don’t really understand.

if lvl.Value >= 5 and lvl.Value < 10 then
			if BadgeService:UserHasBadgeAsync(player.UserId, 2149272660) then return end
			BadgeService:AwardBadge(player.UserId, 2149272660)
			print("got lvl 5 badge")
		elseif lvl.Value >= 10 and lvl.Value < 25 then

So on for the rest of the if statements.

2 Likes

To add on, if you don’t specify, lvl.Value >= 5 will always be true if the player’s level is greater than 5, and the other conditions are not required to be checked.

Is it good to have lvl.Value >= 5 to be always true?

In this case no, you wanted to award the player the badges accordingly, so you must specify the range at which the player will receive the badge.

1 Like

Yeah it works, but also is it necessary for me to have the if BadgeService:UserHasBadgeAsync() then return end line? I just want to make the code more optimized.

The UserHasBadgeAsync is recommended to be in a pcall() dont ask why thats what the roblox documentation says

Oh ok thanks, I guess I should remove them then. I don’t know how to use pcalls though.

How do I specify the range? Do you mean like I should specify the lvl.Value at which the player can receive the badge?

1 Like

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