Hello developers,
Today I need your help designing something. I’m interested in making a system where people can get codes from ads and Twitter and stuff, use them in my game, and get an item.
I know many developers have done this before but I have no idea where to even start, specifically because I want my code system to activate and deactivate codes based on when I want to. For example, the code NEWITEM would activate across all servers at the exact moment I want it to, and deactivate the same way.
How would y’all accomplish this?
1 Like
Well, you could either use assets on the Robles website that contain codes or https service to request them from servers. The other choice is to just publish updates and use a soft shutdown method to prevent losing players.
The easiest way would be to have “pre-approved” codes. For example, you could provide a tick to start and end the code:
local codes = {
NewItem = {
startTime = ,--Input time to start (Use os.time to offset)
length = 2*24*60*60, --2 days length
extraDataSuchAsCoinsToGive = 50,
},
}
local function useCode(code)
local tCode = codes[code]
if tick()>=tCode.startTime and tick()<=tCode.startTime + tCode.length then --In time period
--Here, the code is valid. Use tCode.Coins or other data to reward the code. Also, make sure to save already used codes somewhere.
end
end
3 Likes
My game uses a datastore value instead of it being hard coded, and an OnUpdate.
The above method would also work with that, just make sure to define the time properly. If you really wanted, with your method you could just remove the code from the valid ones.
As mentioned above, there are numerous ways of accomplishing this. For my codes, I use HttpService to make a get request to a Pastebin raw page of the codes in JSON format. I send a request to the page whenever a user redeems a code (with a debounce to prevent spamming) and therefore the codes are updated instantly.
I’ve found some other topics containing some of the other ways to accomplish this. This topic covers how to use the description of a Roblox asset to house your codes. It’s pretty simple and a good starting point if you don’t feel comfortable working with JSON and HttpService. The comments also offer some other ways to make this work.
This similar topic demonstrates a basic plugin that can be used to create and edit codes. It can prevent players from being able to use them more than once and is a great place to get started if you want it basic, but with lots of extra features.
2 Likes
Thanks for the information! Is there a page I could go to that would explain how to use HttpService for this particular implementation? I think I can use it, I just need to understand the syntax.
Ya, some useful pages I used were HttpService :JSONDecode() and :GetAsync() from the wiki. I another one that I didn’t use, but exists is the JSON.org site.
On my Pastebin page, I store the codes like: {"CodeName":["CodeRewardType", "CodeRewardDetails", "CodeRewardText"]}
; where CodeName is the text to be inputted by the player, CodeRewardType is my way of knowing what kind of reward the player gets (Coins, XP Boost), CodeRewardDetails goes further and holds what is given (how many coins, length of XP Boost), and CodeRewardText is what the player sees once redeeming (You received 50 coins).
Here is the basis of what I use to handle the redeeming and requesting of codes:
local CodesStore = {} -- This will hold the codes
local CodesURL == "https://pastebin.com/raw/whatever"
RemoteFunction.OnServerInvoke = function(player, code)
if CodesDebounce == false then -- Refresh the code list
CodesDebounce = true
local Success, Err = pcall(function()
local CodeJSON = HTTPService:GetAsync(CodesURL, false) -- Get the JSON formatted codes
local CodeList = HTTPService:JSONDecode(CodeJSON) -- Decode the JSON
CodesStore = {} -- Empty the old codes table
for Name, Details in pairs(CodeList) do
-- Loop through the decoded JSON and save the codes into the CodesStore table
end
end)
if not Success then
return nil -- Error occurred
end
delay(60, function() -- Allow the codes to be refreshed again once a player tries to redeem one after the debounce
CodesDebounce = false
end)
end
for i,v in pairs(CodesStore) do
-- Loop through code list and give rewards if a match is made
end
-- Handle what is returned to the client (Success, Invalid, etc)
end
2 Likes