This is a relatively short overview on how to get started implementing a code system into your game. With this system, you can update and add codes in live time (short delay ~30s) so you do not have to shutdown servers!
Step 1: Create an asset
For this simple system, we will need to have an assetId handy. To do this, publish a blank model, decal, audio, or place to Roblox. Anything with an assetId that has a description will work!
Step 2: Adding our codes
To store our codes, we will need to use a method of the HttpService. Lets start to build our system:
game:GetService("HttpService")
Now lets create our table of codes that we want to turn into a JSON encoded string.
local codes = {["GEMS"] = {"Gems", 25}}
“GEMS” = The code
“Gems” = The stat we want to change
25 = The value we want to change it by
This table set up allows us to store the code we want players to use as well as two important parameters:
- The type of reward we want to give the player
- The amount of reward we want to give the player
To turn this into a JSON encoded string we can then do the following:
local codes = {['GEMS'] = {'Gems', 25}}
print(game:GetService("HttpService"):JSONEncode(codes))
If we put this into the command line we get this
{"GEMS":["Gems",25]}
Now, to add more codes, we don’t need to repeat this process. Since it is already set up, we can just add new codes like this:
{"GEMS":["Gems",25],"COINS":["Coins",25]}
Because this is how JSON string are encoded, we can be assured that this will work. We then copy and past this into the assets description!
Step 3: Accessing our codes
We now have our codes stored in an assets description, but how do we fetch them? It is actually fairly simple. Using the MarketplaceService, we can fetch the description using the assetId like so:
local description = game:GetService("MarketplaceService"):GetProductInfo(assetId).Description
To decode the JSON string back into a table we can use:
local description = game:GetService("MarketplaceService"):GetProductInfo(assetId).Description
local codes = game:GetService("HttpService"):JSONDecode(desc)
Because we are using JSONDecode, codes is a table. We can now easily search through the codes to see if the right one was called. Here is the finished product of our code system. Remember to keep this on the server, you wouldn’t want to leak the assetId where you store your codes!
local function codes(player, code)
local codes = game:GetService("HttpService"):JSONDecode(game:GetService("MarketplaceService"):GetProductInfo(assetId).Description)
local stats = PLAYERS_STATS
-- check if code was already redeemed in your DataStore
if(codes[code])then
stats[codes[code][1]].Value = stats[codes[code][1]].Value + codes[code][2]
return true
else
-- code doesn't exist
return false
end
end
Warnings:
The provided code may not fit your data structure. If you need assistance implementing the changing of stats, please feel free to send me a message!
TIPS:
If you do not feel comfortable using an asset to store codes, you can also do something more advanced without changing most of the code! Because we are decoding a JSON string, we can change our MarketplaceService call to a HttpService:GetAsync() request to a web server!
If you think I missed or overlooked anything, please feel free to send me feedback, this is my first time making a tutorial