Gatcha System Not Working

Hey!

Im trying to make a gatcha type system in my roblox game that runs whenever a player purchases a developer product.

The problem i’m currently having is the script doesnt seem to keep rerolling if the player already owns a badge(I tested this by getting all the badges in the table and it still kept awarding me badges i already owned) This is the script:

– Define the badges global
local badges = {
[450] = {“BadgeIDPlaceholder”, “BadgeIDPlaceholder”, “BadgeIDPlaceholder”, “BadgeIDPlaceholder”, “BadgeIDPlaceholder”},
[300] = {“BadgeIDPlaceholder”, “BadgeIDPlaceholder”, “BadgeIDPlaceholder”, “BadgeIDPlaceholder”},
[200] = {“BadgeIDPlaceholder”, “BadgeIDPlaceholder”, “BadgeIDPlaceholder”, “BadgeIDPlaceholder”, “BadgeIDPlaceholder”},
[50] = {“BadgeIDPlaceholder”, “BadgeIDPlaceholder”,},
}

– Function to award a badge to a player
local function awardBadge(player, badge)
– Use the BadgeService to award the badge to the player
game:GetService(“BadgeService”):AwardBadge(player.UserId, badge)
print(player.Name … " was awarded " … badge)
end

– Function to handle a player buying a dev product
local function onPlayerBuyDevProduct(player)

-- Generate a random number from 1 to 1000
local randomNumber = math.random(1000)
-- Determine the percentage based on the random number
local percentage
if randomNumber > 300 then
	print("Player Got Rare Rarity!")
	percentage = 450
elseif randomNumber > 200 then
	print("Player Got Epic Rarity!")
	percentage = 300
elseif randomNumber > 50 then
	print("Player Got Legendary Rarity!")
	percentage = 200
else
	print("Player Got Mythic Rarity!")
	percentage = 50
end

-- Get the badges for this percentage
local percentageBadges = badges[percentage]

if percentageBadges then
	-- Choose a random badge from the badges for this percentage
	

	-- If the player already has the badge, reroll
	local success, message = pcall(function()
		local badge = percentageBadges[math.random(#percentageBadges)]
		print(badge)
		
		if game:GetService("BadgeService"):UserHasBadgeAsync(player.UserId, badge) then
			task.wait(.5)
			awardBadge(player, BadgeIDPlaceholder)
		end
		print("they did not have the badge")
		-- Award the chosen badge to the player
		task.wait(.5)
		if not game:GetService("BadgeService"):UserHasBadgeAsync(player.UserId, badge) then
			awardBadge(player, badge)
		end
	end)

	if not success then
		if game:GetService("BadgeService"):UserHasBadgeAsync(player.UserId, 2147776855) then
			awardBadge(player, BadgeIDPlaceholder)
			print("An error occurred: " .. tostring(message))
			
		else
			awardBadge(player, BadgeIDPlaceholder)
			
		end
	end

else
	print("No badges defined for " .. percentage .. "%")
end

end

– Connect the function to the player buying a dev product event
game:GetService(“MarketplaceService”).ProcessReceipt = function(receiptInfo)
local player = game:GetService(“Players”):GetPlayerByUserId(receiptInfo.PlayerId)
if player then
onPlayerBuyDevProduct(player)
end
– Replace with the appropriate value
return Enum.ProductPurchaseDecision.PurchaseGranted
end