Check on datastore for similar values

I’m trying to script a one-time code system. That will give the players rewards by codes that I generate by myself (on an UI that generates a random code) in-game (random codes like: 123a4-567b8-901c2)

When I generate a code, it is should be saved on a datastore so there can’t be 2 identical codes

So I need help to:
1 - Save the generated code on a datastore
2 - When I generate the code, check if there isn’t the same code already saved
3 - How to completely delete the code that the play use

Thanks

2 Likes

You would want to save the key of the datastore as the generated the code, and the value to a boolean value to see whether the code has been used or not - false for the code not being used yet and true for the code having been used before (for simplicity’s sake, I’m just suggesting a boolean value.

If you want to save more statistics such as who generated the code, you might want to consider saving a table as the value of the key). You can refer to this Doc if you aren’t sure on saving data.

You can use :GetAsync to see if a specific key exists in the Datastore. Since you would have saved the generated code as a key in the Datastore, you can just pass the generated code as an argument into :GetAsync. If it returns nil, the key doesn’t exist, but if it does, you’ll have to make your code re-generate another code.

If the code does exist, you’ll need to check for the value of the key. If it reads false, the code hasn’t been used yet, and the user can progress with being rewarded.

Once a Player uses the generated code, you can use :SetAsync to set the value of the key to true to indicate that the key has been used. That way, the code can never be generated again, or be used for that matter.

2 Likes

I’ll write you out a script and I’ll explain along the way

local code = GenerateCode() — function to create a code
local CodesDataStore = DataStoreService:GetDataStore(“CodesDataStore”) — Get the data store used to save codes
local codetable — This will be an array I think since there doesn’t need to be a key for the code
local key = “UniqueCodes”
local s,e = pcall(function()
   codetable = CodesDataStore:GetAsync(key)
end)
if s and codestable then
   if table.find(codestable,code) then 
      repeat code = GenerateCode() task.wait() until not table.find(codestable,code)
      table.insert(codestable,code)
      CodesDataStore:SetAsync(key,codestable)
   else
      table.insert(codestable,code)
      CodesDataStore:SetAsync(key,codestable)
   end
end
— The following code needs to be in the body of the function that removes the code when redeemed
local redeemedcode = — Put the redeemed code here

local codetable
local key = “UniqueCodes”
local s,e = pcall(function()
   codetable = CodesDataStore:GetAsync(key)
end)
if s and codestable then
   if table.find(codestable,redeemedcode) then 
     table.remove(codestable,table.find(codestable,redeemedcode))
   end
— Save the codestable again (same code as the top)

I know this is messy but I’ll fix it once I’m on my pc

2 Likes

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