Script being weird ; Awarding badge when not meeting the right level

Heyy! So in my game I got this code:

local awardXp = function(player)
	local Counter = 0
	while true do
		if player then
			task.wait(15)
			warn("Still running the task.wait 15")
			Counter += 15

			if not (player:FindFirstChild("PlayerData") and player:FindFirstChild("leaderstats")) then
				continue
			end

			player.PlayerData.Playtime.Value += 15

			if Counter < 90 then 
				continue
			end

			if not (player.PlayerData:FindFirstChild("XP") and player.PlayerData:FindFirstChild("Level")) then
				continue
			end
			if (game.PrivateServerId == "" and game.PrivateServerOwnerId == 0) then
				local baseXP = (if (player.MembershipType == Enum.MembershipType.Premium) then 150 else 100)
				local passMult = (if mps:UserOwnsGamePassAsync(player.userId, 189517075) then 2 else 1)
				player.PlayerData.XP.Value += (baseXP * passMult)
			end

			if player.PlayerData.XP.Value < (player.PlayerData.Level.Value) * 100 then
				continue
			end

			player.PlayerData.Level.Value += 1
			player.leaderstats.Level.Value = player.PlayerData.Level.Value
			local character = player.Character
			
			if (character and character.Head:FindFirstChild("GroupRank")) then
				if not (player:FindFirstChild("ResetChatTag")) then
					player.Character.Head.GroupRank.Level.Text = "Level "..player.PlayerData.Level.Value..""
				end
			end

			player.PlayerData.XP.Value = 0
			
			if player.PlayerData.Level.Value == 5 then
				print("AWARDED BADGE: 5", player.PlayerData.Level.Value)
				awardBadge(player.UserId,{5})
			elseif player.PlayerData.Level.Value == 25 then
				print("AWARDED BADGE: 5", player.PlayerData.Level.Value)
				awardBadge(player.UserId,{25})
			elseif player.PlayerData.Level.Value == 50 then
				print("AWARDED BADGE: 5", player.PlayerData.Level.Value)
				awardBadge(player.UserId,{50})
			elseif player.PlayerData.Level.Value == 100 then
				print("AWARDED BADGE: 5", player.PlayerData.Level.Value)
				awardBadge(player.UserId,{100})
			elseif player.PlayerData.Level.Value == 250 then
				print("AWARDED BADGE: 5", player.PlayerData.Level.Value)
				awardBadge(player.UserId,{250})
			elseif player.PlayerData.Level.Value == 500 then
				print("AWARDED BADGE: 5", player.PlayerData.Level.Value)
				awardBadge(player.UserId,{500})
			elseif player.PlayerData.Level.Value == 750 then
				print("AWARDED BADGE: 5", player.PlayerData.Level.Value)
				awardBadge(player.UserId,{750})
			elseif player.PlayerData.Level.Value == 1000 then
				print("AWARDED BADGE: 5", player.PlayerData.Level.Value)
				awardBadge(player.UserId,{1000})
				--local messageToSent = player.Name.."]DIVIDER{}[767"..tostring(player.UserId)
				--game:GetService("MessagingService"):PublishAsync("Run1kLevelEvent",messageToSent)
			end

			if Counter >= 90 then Counter = 0 end
		else
			--coroutine.yield()
			break
		end

	end
end

I run this function once a player joins and everything works normal. However, I’ve had the case that somenone was Level 43 and got awarded Badge 50. This is the badge code:

local function awardBadge(userId,tableOfBadges)
	local badges = {
		[5] = 1327925323341428,
		[25] = 594863095355604,
		[50] = 116012729846607,
		[100] = 1425116120184433,
		[250] = 4255769407063389,
		[500] = 2108712611193156,
		[750] = 3468318180032257,
		[1000] = 1429102176634570,
	}
	
	for index,value in pairs(tableOfBadges) do
		badgeService:AwardBadge(userId,badges[value])
	end
end

Does someone see why because I genuinely don’t know. My UI updates to the player.PlayerData.Level too and that works perfectly.

1 Like

There is already an issue that caught my eye. It seems you are checking after updating the level that if the badge(s) should be given matching the exact number of the level without storing about it anything. Although that can work, but also has it’s downs. I would suggest you to refactor your code to store the last value before updating the level, and then update the level and store that value as well. Afterwards you can just call down the awardBadge function after doing a calculation and the results would show that the levels are okay to award the badge(s).

2 Likes