Advice needed regarding checking badge ownership

Hello!

I currently have a script which iterates through each badge and checks if the player owns it, however I have run into the problem where it creates a lot of requests, and I plan on adding quite a few more badges which I fear might cause errors. It currently works as expected but I think switching methodology might be a good idea as well.

So I’m brought to a dilemma, should I:

a) create a badge table inside of the player’s data to show what badges were awarded, or
b) keep going how I’m going right now

So here’s what my badge checker looks like right now, and keep in mind that I have around 10 badges right now:

local function checkBadges(player)
	local leaderstats = player:WaitForChild('leaderstats')
	local stage = leaderstats:WaitForChild('Stage')
	local data = playerData:WaitForChild(player.UserId)
	if stage.Value >= 10 then
		local success, userOwnsBadge = pcall(badgeService.UserHasBadge, badgeService, player.UserId, 2124801810)
		if success and not userOwnsBadge then
			pcall(badgeService.AwardBadge, badgeService, player.UserId, 2124801810)
		end
	end
	for i,v in pairs(difficultyHandler) do
		if type(v) == 'table' and v.Stages.Max + 1 <= stage.Value then
			local success, userOwnsBadge = pcall(badgeService.UserHasBadge, badgeService, player.UserId, v.Badge)
			if success and not userOwnsBadge then
				pcall(badgeService.AwardBadge, badgeService, player.UserId, v.Badge)
			elseif not success then
				warn('Couldn\'t get badge status because',userOwnsBadge)
			end
		end
	end
	local inventory = data:WaitForChild('Inventory')
	for i,v in pairs(badges) do
		if inventory:FindFirstChild(i) then
			local success, userOwnsBadge = pcall(badgeService.UserHasBadge, badgeService, player.UserId, v)
			if success and not userOwnsBadge then
				pcall(badgeService.AwardBadge, badgeService, player.UserId, v)
			end
		end
	end
end

Thanks for any insight!

I don’t know if one will have better performance over another, but I do know that creating a table will make it easier to add or remove badges.

By inside of the player’s data I mean it saves to the datastore, then upon awarding the badge, create a value inside of the table to save. It would probably create less requests but I think it might be less reliable. However, by less reliable I mean there’s a chance their badge won’t be added but there’s no chance a badge they don’t own will be added so I could just try to award it again.

I also agree with your statement regarding organization, I think I’m more on the side of making a table to be saved since their data is going to be saved anyway, however it is going to contribute to the data limits so that’s where I’m stuck again (although 4 million characters is quite hard to reach).

What do you think?