How to make a 1 time redeemable code for badge?

So I have this very simple GUI where you can type a code and if you type the correct code you are awarded a badge, but I want to choose the number of people that are able to get this code “So for example, only 1 person can redeem this code (and they will get the badge for redeeming it first), and then anyone that tries to redeem it after that 1st person already redeemed it, won’t get the badge”.

How do I make that work? (I wanted to make it so that its global, as in the first person in all servers, as the code is a 1-time use only and then it expires forever)

Here is what the GUI looks like:

Frame2 is basically what shows if you enter the correct code:

This is how the local script currently looks like:

local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")

local BADGE_ID = 2146480330


script.Parent.MouseButton1Click:Connect(function()
	if script.Parent.Parent.TextBox.Text == "JD93CGKG" then
		script.Parent.Parent.Parent.Frame2.Visible = true
		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 awardSuccess, result = pcall(function()
						return BadgeService:AwardBadge(player.UserId, badgeId)
					end)

					if not awardSuccess then
						-- the AwardBadge function threw an error
						warn("Error while awarding badge:", result)
					elseif not result then
						-- the AwardBadge function did not award a badge
						warn("Failed to award badge.")
					end
				end
			else
				warn("Error while fetching badge info: " .. badgeInfo)
			end
		end

		local function onPlayerAdded(player)
			awardBadge(player, BADGE_ID)
		end
		
		Players.PlayerAdded:Connect(onPlayerAdded)
		
	end
end)

(As seen in the LocalScript, the code, in this case, is “JD93CGKG”)

1 Like

You can create a datastore with redeemed badges or the opposite - datastore with redeemable badges. Obviously u add or remove the badge from datastore once claimed.

1 Like

Actually, had a change of plans and wanted to do something else, but I was wondering if you have any idea why the above script isn’t working? I enter the correct code but I don’t get awarded the badge & I don’t get anything in output so I’m not sure what happened wrong…

1 Like

This part right here:

		local function onPlayerAdded(player)
			awardBadge(player, BADGE_ID)
		end
		
		Players.PlayerAdded:Connect(onPlayerAdded)

makes it so the badge is only awarded to people who join the game.

So, essentially, what did is you made a text box that, when someone enters the code, everybody that joins the server afterwards gets the badge.

Instead, replace this part with just simply:

awardBadge(player, BADGE_ID)

Another issue with this is that it is in a LocalScript, which cannot award badges.
Instead, you should send a remote event to the server with the input from the text box and check to see if the input matches with the code, and award the badge if it does. It’s important that you check the code on the server and not the client, as exploiters could abuse that.

So it should look something more like this:
(local script)
|
v

local Players = game:GetService("Players")
local event = --insert location of your remote event here

script.Parent.MouseButton1Click:Connect(function()
	event:FireServer(script.Parent.Parent.TextBox.Text)
end)

(server script)
|
v

local BadgeService = game:GetService("BadgeService")
local BADGE_ID = 2146480330
local event = --insert location of remote event here

event.OnServerEvent:Connect(function(player, code)
	if code == "JD93CGKG" then
		--put the award badge function here
	end
end)

Hopefully I haven’t made a mistake and this is helpful and educational

1 Like

Worked just fine, and I super appreciate the thorough explanation, thank you so much!

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