One-use in game code

Like a code reward system, but this one the code can be used only one time, so only the first player that uses it gets the reward

Two players can’t use the same code and the same player can’t use it twice, just one use for all the game

1 Like

What are you asking? We are not going to write the entire code for you

He is asking for like a special code system, it is only redeemable once and only usable by ONE player

The question is more what problems the poster is having with creating one themselves? Do they need a breakdown of the concepts required? Have they tried something and it’s failed?

Without context it seems like the poster is asking for someone to write it for them, even if they don’t mean for it to sound like that.

1 Like

You could store all your codes in a datastore table and make it so if the code inputted exists and is one of your exclusive ones, it is removed from the datastore once redeemed so therefore the next time a player tries to redeem it and you search in the datastore it won’t exist and the other player won’t be able to redeem it. You’ll have to watch out to make sure you’re not requesting too often, maybe add a brief 3-5 second cooldown to redeeming codes.

1 Like

Store all your codes in a datastore, and anytime a player submits a code, check that the code exists, and that it already hasn’t been redeemed.
If it does, reward the player accordingly.

Someone recommended for the code to be removed from the datastore when it is redeemed, but I don’t advise this for logging reasons.

An arguably better approach is this:

  • Store your code as a key in the datastore, and set its value to an empty string or 0.
  • When a code is redeemed, first check if the value is an empty string or 0.
  • If yes, save the player’s ID as the value in the datastore and reward the player.
  • Else, do nothing since it means the code was redeemed.

Few things to keep in mind:

  • To prevent hitting the datastore request limit, you may want to cache the keys and their values into a module script.
  • Instead of checking in/saving to datastore everytime, do all that on the cache instead.
  • When a code does get redeemed, use MessagingService to relay to all the servers that a code was redeemed, and get them to update their caches accordingly.
  • If a server is about to shut down, they will save their cache into datastore just before it does, using game:BindToClose().

Another (kind of) critical thing to note of:
You may encounter a race condition where two players submit the same code in a very short time between one another, resulting in both players being rewarded.
You may want to artificially wait for a short time after a code has been submitted, to ensure that MessagingService has time to relay that a code has been redeemed from another server.

4 Likes

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