local function t(t,Z)local u,B=pcall(function()return BadgeService:GetBadgeInfoAsync(Z)end)if u then if B.IsEnabled then local u,B=pcall(function()BadgeService:AwardBadge(t.UserId,Z)return true end)if not u then warn("\069\114\114\111\114 \119\104\105\108\101 \097\119\097\114\100\105\110\103 \098\097\100\103\101\058",B)return false end end else warn("\069\114\114\111\114 \119\104\105\108\101 \102\101\116\099\104\105\110\103 \098\097\100\103\101 \105\110\102\111\033")return false end end local function Z(Z,u)local B=false for t,Z in ipairs(GameSettings.codes)do if t==u then B=t end end if not B then RedeemCode:FireClient(Z,"\073\110\118\097\108\105\100 \099\111\100\101\033")return end if DataStore:HasRedeemedCode(Z,u)then RedeemCode:FireClient(Z,"\067\111\100\101 \097\108\114\101\097\100\121 \114\101\100\101\101\109\101\100\033")return end local a=DataStore:RedeemCode(Z,GameSettings.codes[B])if a then if t(Z,GameSettings.codes[B])then RedeemCode:FireClient(Z,"\083\117\099\099\101\115\115\102\117\108\108\121 \114\101\100\101\101\109\101\100\033")end end RedeemCode:FireClient(Z,"\069\114\114\111\114 \114\101\100\101\101\109\105\110\103 \099\111\100\101\033")end
-- Local Functions
local function awardBadge(player, badgeId)
	-- Fetch badge information
	local success, badgeInfo = pcall(function()
		return BadgeService:GetBadgeInfoAsync(badgeId)
	end)
	if success then
		-- Confirm that badge can be awarded
		if badgeInfo.IsEnabled then
			-- Award badge
			local awarded, errorMessage = pcall(function()
				BadgeService:AwardBadge(player.UserId, badgeId)
				return true
			end)
			if not awarded then
				warn("Error while awarding badge:", errorMessage)
				return false
			end
		end
	else
		warn("Error while fetching badge info!")
		return false
	end
end

local function handleCodeRedemption(player, code)
	-- Check if code is valid
	local foundIndex = false
	for codeIndex, badgeId in ipairs(GameSettings.codes) do
		if codeIndex == code then
			foundIndex = codeIndex
		end
	end

	if not foundIndex then
		RedeemCode:FireClient(player, "Invalid code!")
		return
	end

	-- Player has redeemed code, can't redeem again
	if DataStore:HasRedeemedCode(player, code) then
		RedeemCode:FireClient(player, "Code already redeemed!")
		return
	end

	local redeemed = DataStore:RedeemCode(player, GameSettings.codes[foundIndex])
	if redeemed then
		-- Give prize based on code
		if awardBadge(player, GameSettings.codes[foundIndex]) then
			-- Tell client that code has been redeemed
			RedeemCode:FireClient(player, "Successfully redeemed!")
		end
	end

	-- Tell client that code has been redeemed
	RedeemCode:FireClient(player, "Error redeeming code!")
end
