How to make a one time use code

How can you make a ONE TIME CODE? Basically, What I mean is a developer gives you a code to redeem, Once that player uses the code ONCE, The code is always for them and no one else can use it
image

5 Likes

You can make an API that generates codes (the text box can check if they are valid, or used already through the API)

1 Like

Store the code in MemoryStoreService and delete the key after they redeem it

2 Likes

How can you do that? And how can it be secure?

1 Like

Have a table with all the codes you want players to redeem only once, when a player writes a code you check if its inside the table (if it is valid or not). If its valid and in the table, once the player redeems the code you remove it from the table so its no longer a valid code for everyone else. You can store the code in a string value inside the player if you want them to keep what code they used

1 Like

How can you set up a table? Sorry lol

1 Like

I know that RapidAPI is secure… you can set up APIs through there I think…

1 Like
local CodesTable = {

"Code1",
"Code2",
"Code3"
}

Put the table in a serverScript or (preferably) a ModuleScript

2 Likes

I already have a table of all the codes in a module on the server, How can you tell the script that this specific player has access to redeem it and how other players don’t?

1 Like
local Codes = {
	["ABC"] = {"Username"}
}

local Player = Player
local Code = "ABC"

if table.find(Codes[Code],Player.Name) then
	print("They can redeem it yay")
end
2 Likes

First of all you should have a local script which grabs the code the player has entered into their CodesUI.

Then you check if the string they typed exists in the ModuleTable

Local script:

submit.MouseButton1Click:Connect(function()
   game.ReplicatedStorage.SubmitCodeRemoteEvent:FireServer(textbox.Text)
end)

Server script

local CodesTable = require(script.ModuleScript)
-- replace ModuleScript with the name of your codes' Module Script

game.ReplicatedStorage.SubmitCodeRemoteEvent.OnServerEvent:Connect(function(player, code)
   if CodesTable[code] then
    print("Code Redeemed succesfully!")
    table.remove(CodesTable, code) -- removes the code from the list of valid codes so no one else can use it
    -- Do anything else you want with the redeemed code
   end
end)
4 Likes

Thank you, It works now! Very much appreciated!

1 Like

Careful though as this would only work for the server in which the code was redeemed in, since you’re only removing the code from the module script inside of the server where the code was redeemed in. Players in any other server could still redeem the code without problem, since the code still exists in the table there (it didn’t get removed since the event only got fired in the one server where a player redeemed the code). I don’t think you can bypass using DataStores to make the code be one-time only across all servers (MemoryStoreService might also work, however I don’t have much experience with that).

2 Likes

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